JPQL Advanced Operations

Using JPQL, we can perform any type of database operations. Here, we will perform some advanced operations of JPQL using simple examples.

Let us consider the student table having the following records.








JPQL Advanced Query Examples

In this example, we will take a basic entity class (in this case StudentEntity.java) and perform different operations on it.

Step 1. Create an entity class named as StudentEntity.java under com.javahubpoint.jpa package.

StudentEntity.java

package com.javahubpoint.jpa;  
import javax.persistence.*;  
  
@Entity  
@Table(name="student")  
public class StudentEntity {  
  
    @Id  
    private int s_id;  
    private String s_name;  
    private int s_age;  
      
    public StudentEntity(int s_id, String s_name, int s_age) {  
        super();  
        this.s_id = s_id;  
        this.s_name = s_name;  
        this.s_age = s_age;  
    }  
  
    public StudentEntity() {  
        super();  
    }  
  
    public int getS_id() {  
        return s_id;  
    }  
  
    public void setS_id(int s_id) {  
        this.s_id = s_id;  
    }  
  
    public String getS_name() {  
        return s_name;  
    }  
  
    public void setS_name(String s_name) {  
        this.s_name = s_name;  
    }  
  
    public int getS_age() {  
        return s_age;  
    }  
  
    public void setS_age(int s_age) {  
        this.s_age = s_age;  
    }  
      
}  

Step 2.  Now, map the entity class and other databases configuration in Persistence.xml file.

Persistence.xml

<persistence>  
<persistence-unit name="Student_details">  
     
      <class>com.javahubpoint.jpa.StudentEntity</class>  
  
      <properties>  
         <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>  
         <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/studentdata"/>  
         <property name="javax.persistence.jdbc.user" value="root"/>  
         <property name="javax.persistence.jdbc.password" value=""/>  
         <property name="eclipselink.logging.level" value="SEVERE"/>  
         <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>  
      </properties>  
        
   </persistence-unit>  
</persistence>  

Step 3.  Now, we can perform any of the following operations on StudentEntity.java class.

1. JPQL Filter

Here, we will perform some filter operations on a table.

Filter.java

package com.javahubpoint.jpa.jpql;  
import com.javahubpoint.jpa.StudentEntity;  
import javax.persistence.*;  
import java.util.*; 

public class Filter {  
      
     public static void main( String args[]) {  
             
          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( ); 
          
        Query q1 = em.createQuery("Select s from StudentEntity s where s.s_age between 22 and 28");  
       
        @SuppressWarnings("unchecked")  
        List<StudentEntity> l1 =  (List<StudentEntity>)q1.getResultList();  
           
         System.out.println("Between Clause");  
         System.out.print("s_id");  
         System.out.print("\t s_name");  
         System.out.println("\t s_age");  
           
          for(StudentEntity s:l1)  
          {  
          System.out.print(s.getS_id());  
          System.out.print("\t"+s.getS_name());  
          System.out.println("\t"+s.getS_age());  
          }
            
          Query q2 = em.createQuery("Select s from StudentEntity s where s.s_age IN(20,22,23)");  
           
            @SuppressWarnings("unchecked")  
            List<StudentEntity> l2 =  (List<StudentEntity>)q2.getResultList();  
               
             System.out.println("IN Clause");  
             System.out.print("s_id");  
             System.out.print("\t s_name");  
             System.out.println("\t s_age");  
               
              for(StudentEntity s:l2)  
              {  
              System.out.print(s.getS_id());  
              System.out.print("\t"+s.getS_name());  
              System.out.println("\t"+s.getS_age());  
              }  
                
              Query q3 = em.createQuery("Select s from StudentEntity s where s.s_name like '%a%'");  
               
                @SuppressWarnings("unchecked")  
                List<StudentEntity> l3 =  (List<StudentEntity>)q3.getResultList();  
                  
                 System.out.println("Like Clause");  
                 System.out.print("s_id");  
                 System.out.print("\t s_name");  
                 System.out.println("\t s_age");  
                   
                  for(StudentEntity s:l3)  
                  {  
                  System.out.print(s.getS_id());  
                  System.out.print("\t"+s.getS_name());  
                  System.out.println("\t"+s.getS_age());  
                  }  
            
          em.getTransaction().commit();  
          em.close();  
          emf.close();    
     }  
}  

Output:


2. JPQL Aggregate

Here, we will perform some aggregate operations on a table.

Aggregate.java

package com.javahubpoint.jpa.jpql;  
import com.javahubpoint.jpa.StudentEntity;  
import javax.persistence.*;  
import java.util.*;  
public class Aggregate {  
      
     public static void main( String args[]) {  
             
          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( );
            
          Query q1 = em.createQuery("Select count(s) from StudentEntity s");  
          System.out.println("Number of Student : "+q1.getSingleResult());  
               
          Query q2 = em.createQuery("Select MAX(s.s_age) from StudentEntity s");  
          System.out.println("Maximum age : "+q2.getSingleResult());  
            
          Query q3 = em.createQuery("Select MIN(s.s_age) from StudentEntity s");  
          System.out.println("Minimum age : "+q3.getSingleResult());  
em.getTransaction().commit();  
          em.close();  
          emf.close();    
     }  
}  

Output:


3. JPQL Sorting

Here, we will sort the elements of table on the basis of s_age attribute.

Sorting.java

package com.javahubpoint.jpa.jpql;  
import com.javahubpoint.jpa.StudentEntity;  
import javax.persistence.*;  
import java.util.*;

public class Sorting {  
      
     public static void main( String args[]) {  
             
          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( );  
            
          Query q1 = em.createQuery("Select s from StudentEntity s order by s.s_age desc");  
           
            @SuppressWarnings("unchecked")  
            List<StudentEntity> l1 =  (List<StudentEntity>)q1.getResultList();  
               
             System.out.print("s_id");  
             System.out.print("\t s_name");  
             System.out.println("\t s_age");  
               
              for(StudentEntity s:l1)  
              {  
              System.out.print(s.getS_id());  
              System.out.print("\t"+s.getS_name());  
              System.out.println("\t"+s.getS_age());  
              }  
               
          em.getTransaction().commit();  
          em.close();  
          emf.close();    
     }  
}  

Output: