Given a table, say:
orderId | typeId
----------------
1 | 1
2 | 1
3 | 2
4 | 5
5 | 1
6 | 2
And I have an Order object which has a Type object (Type has a Set of Orders in the other direction.
How would I go about retrieving the most popular Type of Order (in this case, type 1) using Hibernate’s Criteria API?
you might wanna look into Projections. You should be able to use Projections.groupProperty() on the “type” association, and add up the results.
check this SO question: Expressions in hibernate criteria
edit: tried it and the following seems to work fine:
Order:
Type:
and the Criteria: