문제

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?

도움이 되었습니까?

해결책

The question is extremely unclear. So here is a vague answer:

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);

다른 팁

I think, it's the answer!

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

And it's a constructor of entities.PersonWell class:

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;
    }

There's some code for an extraction of result:

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

I hope, It'll help you! :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top