1

I need to assign the result of a jpql result to a simple class java object

I have something like this

class myObject() {
@id
private Long id;
private String Name;
private String description;
...
//getters and setters
}

I need to somehow to store the result of this SQL query, example

// could be anytable SELECT DISTINCT c.table_id, c.name, NULL AS description FROM anyTable

How do I do this in JPQL and then assign the result to my object?

4

2 回答 2

1

这个问题非常不清楚。所以这里是一个模糊的答案:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPu");
EntityManager em = emf.createEntityManager();    
Query q = em.createQuery("select f.id, f.name from Foo f");
List<Object[]> foos = (List<Object([]>)q.getResultList();

MyObject o = new MyObject();
o.setFoos(foos);
于 2010-09-02T21:25:43.660 回答
0

我想,这就是答案!

SELECT NEW entities.PersonWell(c.peopleId, c.name, c.age, c.height, c.weight)
FROM 
  People AS c;

它是entities.PersonWell 类的构造函数:

public PersonWell(Integer peopleId, String name, short age, short height, short weight, short speechspeed) {
        this.peopleId = peopleId;
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.speechspeed = speechspeed;
    }

有一些用于提取结果的代码:

List<PersonWell> resultList = query.getResultList();

我希望,它会帮助你!:)

于 2013-03-06T15:22:40.947 回答