File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes JPA 2: In a query, how to compare the size of lists? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "JPA 2: In a query, how to compare the size of lists?" Watch "JPA 2: In a query, how to compare the size of lists?" New topic
Author

JPA 2: In a query, how to compare the size of lists?

Tagn Sip
Greenhorn

Joined: Sep 03, 2010
Posts: 1
Hey all,

in a query, i wanna compare the size of two lists I have, lets say:

public class school{

List<pupil> pupils = new ArrayList();

}
....

And now i wanna get an descending ordered list of the schools in my DB, depending on their pupils-count( => size). I believe there was a way to get the size of a list in a query, but I'm not sure and i can't find a solution at the moment. I already tried the whole thing by using Group By on the schools and then order the schools by count(pupils)... but my DB(JDBC derby from NetBeans) doens't support aggregate functions in an Order By clause...

Thanks for help.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 421
Try any of,

--Using SIZE function (uses sub-select in SQL)

SELECT e FROM Employee ORDER BY SIZE(e.projects) DESC

--Using SIZE function, also selects the size (uses group by)

SELECT e, SIZE(e.projects) FROM Employee ORDER BY SIZE(e.projects) DESC

--Using GROUP BY

SELECT e, COUNT(p) FROM Employee JOIN e.projects p ORDER BY COUNT(p) DESC

--Using GROUP BY and alias

SELECT e, COUNT(p) AS pcount FROM Employee JOIN e.projects p ORDER BY pcount DESC

See,
http://en.wikibooks.org/wiki/Java_Persistence/Querying#How_to_order_by_the_size_of_a_collection

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
 
subject: JPA 2: In a query, how to compare the size of lists?
 
developer file tools