Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order by count() in JPA

I am using This JPA-Query:

SELECT DISTINCT e.label FROM Entity e 
GROUP BY e.label 
ORDER BY COUNT(e.label) DESC

I get no errors and the results are sorted almost correct but there are some values wrong (either two values are flipped or some single values are completly misplaced)

EDIT:

Adding COUNT(e.label) to my SELECT clause resolves this problem for this query.

But in a similar query which also contains a WHERE clause the problem persists:

SELECT DISTINCT e.label, COUNT(e.label) FROM Entity e 
WHERE TYPE(e.cat) = :category 
GROUP BY e.label 
ORDER BY COUNT(e.label) DESC
like image 691
Martin Schlagnitweit Avatar asked Aug 09 '11 18:08

Martin Schlagnitweit


People also ask

How do I sort by criteria in JPA?

Sorting With JPA Criteria Query Object API With JPA Criteria – the orderBy method is a “one stop” alternative to set all sorting parameters: both the order direction and the attributes to sort by can be set. Following is the method's API: orderBy (CriteriaBuilder.asc): Sorts in ascending order.

What is the orderby method in JPA?

With JPA Criteria – the orderBy method is a “one stop” alternative to set all sorting parameters: both the order direction and the attributes to sort by can be set. Following is the method's API: orderBy(CriteriaBuilder.asc): Sorts in ascending order. orderBy(CriteriaBuilder.desc): Sorts in descending order.

How to count the number of Records in JPA hibernate?

Hibernate: select count(*) as col_0_0_ from products product0_ Note that Spring Data JPA (Hibernate) produces the SQL query to count the number of records that exist in the database table. If playback doesn't begin shortly, try restarting your device.

What is static order in JPA with example?

Here are examples of static order that we can write in a query or using method names, Order with method name as per spring boot JPA standards : Order with the query as like normal SQL standers.


2 Answers

You might need to include the COUNT(e.label) in your SELECT clause:

SELECT DISTINCT e.label, COUNT(e.label) 
FROM Entity e 
GROUP BY e.label 
ORDER BY COUNT(e.label) DESC

UPDATE: Regarding the second query please read section 8.6. Polymorphic queries of the EntityManager documentation. It seems that if you make your queries in a way that requires multiple SELECTs, then the ORDER BY won't work anymore. Using the TYPE keyword seems to be such a case. A quote from the above link:


The following query would return all persistent objects:
from java.lang.Object o // HQL only

The interface Named might be implemented by various persistent classes:

from Named n, Named m where n.name = m.name // HQL only

Note that these last two queries will require more than one SQL SELECT. This means that the order by clause does not correctly order the whole result set. (It also means you can't call these queries using Query.scroll().)


like image 158
MicSim Avatar answered Oct 19 '22 11:10

MicSim


For whatever reason the following style named query didn't work for me:

SELECT DISTINCT e.label, COUNT(e.label) 
FROM Entity e 
GROUP BY e.label 
ORDER BY COUNT(e.label) DESC

It could be because I am using an old version of Hibernate. I got the order by working by using a number to choose the column to sort by like this:

SELECT DISTINCT e.label, COUNT(e.label) 
FROM Entity e 
GROUP BY e.label 
ORDER BY 2 DESC
like image 42
DavidW Avatar answered Oct 19 '22 09:10

DavidW