99
JPA Criteria GROUP BY Clause
The GROUP BY clause is used to collect data from one or more tables and arrange them in a group. In Criteria API, the groupBy() method of AbstractQuery interface is used to filter the records and group them.
Criteria GROUP BY Example
Here, we will perform GROUP BY operations on student table. Let us assume the table contains the following records: –
Now, follow the below steps to perform operations: –
- Create an entity class names as StudentEntity.java under com.tutoraspire.jpa package. This class contains three attributes s_id, s_name and s_age with all the required annotations.
StudentEntity.java
- Now, map the entity class and other databases confiuguration in Persistence.xml file.
Persistence.xml
- Once, we created the basic entity class and mapped the configuration into persistence.xml file, we can perform the different types of GROUP BY operation. Here, we are grouping the number of students on the basis of age.
StudentGroup.java
Output:
Next TopicJPA JPQL Having clause