Criteria group by clause

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Just like JPQL, Criteria group by clause is used to group and filter the results.

Criteria group by clause:

In our article about jpa group by clause, we’ve seen how jpql can be used to group the number of teachers according to department name. It was as follows.

Relationship between Department and Teacher

Relationship between Teacher and Department
Relationship between Teacher and Department

Data in Teacher table.

3 Prasad Kharkar 20000 6
4 Sushsant Pangarkar 40000 6
5 Pranil Gilda 20000 8

Data in Department table

4 Mathematics
6 Science
8 Language

A similar criteria group by clause can be constructed with code below.

Here, multiselect() method is used because we are going to get compound result and not one entity type. We have seen this in selecting multiple results using object arrays. We are grouping the number of teachers working for each department according to department name. Above criteria group by clause query is executed as follows.

This prints out the results as follows.

Language 1
Science 2

I hope this example helped understand criteria group by clause.

References for further study :

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *