Query q1 = em.createQuery("SELECT e.name, e.salary FROM Employee e");
returns List of Object[] instances. OK, what if we would like to return something much simpler then our original Employee object but still more concrete then general Object type? I found there is one very easy and effective solution:
Query q = em.createQuery(
"SELECT " +
"NEW mypackage.EmployeeDetail(e.id, e.name, e.salary) " +
"FROM Employee e");
List<EmployeeDetail> result = q.getResultList();
The mypackage.EmployeeDetail object is just POJO object with particular constructor without any JPA annotations or mapping file.
(Source: Michael Bouschen's Blog - Java Persistence Query Return Types)
2 comments:
What type(class) is em of?
It is EntityManager.
Post a Comment