Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JPA Certification (OCEJPAD)
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
JPA Certification (OCEJPAD)
Modification for sqlResultSetMapping in Chapter 11
Himai Minh
Bartender
Posts: 2447
13
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In chapter 11, sqlResultSetMappings example,
In OrgStructure.java:
public List findDepartmentSummary() { // Derby can't handle this query, use JP QL for this example. Query query = em.createQuery("SELECT d , m, COUNT(e), AVG(e.salary) " + "FROM Department d LEFT JOIN d.employees e " + "LEFT JOIN d.employees m " + "WHERE m IN (SELECT de.manager " + "FROM Employee de " + "WHERE de.department = d) " + "GROUP BY d, m"); return query.getResultList(); }
@SqlResultSetMapping( name="DepartmentSummary", entities={@EntityResult(entityClass=Department.class, fields=@FieldResult(name="name", column="DEPT_NAME")), @EntityResult(entityClass=Employee.class)}, columns={@ColumnResult(name="TOT_EMP"), @ColumnResult(name="AVG_SAL")} ) }) public class Employee { ....
I think the findDepartmentSummary should be modified into:
public List findDepartmentSummary() { // Derby can't handle this query, use JP QL for this example. // Instead of Select d , m... // do this : select d.name as DEPT_NAME as the sqlResultSetMapping specifies name and DEPT_NAME for the entity result. Query query = em.createQuery("SELECT d.name as DEPT_NAME, m, COUNT(e), AVG(e.salary) " + "FROM Department d LEFT JOIN d.employees e " + "LEFT JOIN d.employees m " + "WHERE m IN (SELECT de.manager " + "FROM Employee de " + "WHERE de.department = d) " + "GROUP BY d, m"); return query.getResultList(); }
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
I like...
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks for sharing all your mysql findings!
Have a
Cow
!
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JPQL help
Native Queries
Difference between Fetch Join and inner join
@ColumnResult
Doubt about native query
More...