Criteria WHERE Clause

The WHERE clause is used to apply conditions on database and fetch the data on the basis of that condition. 

In Criteria API, the where() method of AbstractQuery interface is used to set conditions.

Criteria WHERE Example

Here, we will perform several WHERE operations on student table. Let us assume the table contains the following records: -







Now, follow the below steps to perform operations: -

Step 1. Create an entity class names as StudentEntity.java under com.javahubpoint.jpa package. This class contains three attributes s_id, s_name, s_age with all the required annotations.

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.  Once, we have created the basic entity class and mapped the configuration into persistence.xml file, we can perform the different types of select operations in the following ways: -

1. JPQL Greater Than and Less Than

Comparison.java

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

public class Comparison {
      
    public static void main( String args[]) {  
             
         EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( );  
            
          CriteriaBuilder cb=em.getCriteriaBuilder();  
            
          AbstractQuery<StudentEntity> cq1=cb.createQuery(StudentEntity.class);  
          AbstractQuery<StudentEntity> cq2=cb.createQuery(StudentEntity.class);  
            
         Root<StudentEntity> stud1=cq1.from(StudentEntity.class);  
           
        cq1.where(cb.greaterThan(stud1.get("s_age"), 22));  
          
          CriteriaQuery<StudentEntity> select1 = ((CriteriaQuery<StudentEntity>) cq1).select(stud1);  
          TypedQuery<StudentEntity> tq1 = em.createQuery(select1);  
          List<StudentEntity> list1 = tq1.getResultList();  
            
          System.out.println("Students having age greater than 22");  
            
          System.out.print("s_id");  
             System.out.print("\t s_name");  
             System.out.println("\t s_age");  
            
          for(StudentEntity s:list1)  
          {  
          System.out.print(s.getS_id());  
          System.out.print("\t"+s.getS_name());  
          System.out.println("\t"+s.getS_age());  
          }  
            
          Root<StudentEntity> stud2=cq2.from(StudentEntity.class);  
               
              
            cq2.where(cb.lessThan(stud2.get("s_age"), 22));  
              
              CriteriaQuery<StudentEntity> select2 = ((CriteriaQuery<StudentEntity>) cq2).select(stud2);  
              TypedQuery<StudentEntity> tq2 = em.createQuery(select2);  
              List<StudentEntity> list2 = tq2.getResultList();  
  
              System.out.println("Students having age Less than 22");  
                
              System.out.print("s_id");  
                 System.out.print("\t s_name");  
                 System.out.println("\t s_age");  
                
              for(StudentEntity s:list2)  
              {  
              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 Between

Between.java

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

public class Between {  
     
    public static void main( String args[]) {
           
         EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( );  
            
          CriteriaBuilder cb=em.getCriteriaBuilder();  
            
          AbstractQuery<StudentEntity> cq=cb.createQuery(StudentEntity.class);  
           
            
         Root<StudentEntity> stud=cq.from(StudentEntity.class);  
           
          
         cq.where(cb.between(stud.get("s_age"), 22, 26)) ;  
          CriteriaQuery<StudentEntity> select = ((CriteriaQuery<StudentEntity>) cq).select(stud);  
          TypedQuery<StudentEntity> tq = em.createQuery(select);  
          List<StudentEntity> list = tq.getResultList();  
            
        System.out.println("Students having age between 22 and 26");  
            
          System.out.print("s_id");  
             System.out.print("\t s_name");  
             System.out.println("\t s_age");  
            
          for(StudentEntity s:list)  
          {  
          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:






3. JPQL Like

Like.java

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

public class Like {  
      
    public static void main( String args[]) {
           
         EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( );  
            
          CriteriaBuilder cb=em.getCriteriaBuilder();  
            
          AbstractQuery<StudentEntity> cq=cb.createQuery(StudentEntity.class); 
            
         Root<StudentEntity> stud=cq.from(StudentEntity.class); 
      
         cq.where(cb.like(stud.get("s_name"), "R%"));  
          CriteriaQuery<StudentEntity> select = ((CriteriaQuery<StudentEntity>) cq).select(stud);  
          TypedQuery<StudentEntity> tq = em.createQuery(select);  
          List<StudentEntity> list = tq.getResultList();  
            
        System.out.println("Students name starting with R");  
            
          System.out.print("s_id");  
             System.out.print("\t s_name");  
             System.out.println("\t s_age");  
            
          for(StudentEntity s:list)  
          {  
          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:







4. JPQL In

In.java

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

public class In.java {
      
    public static void main( String args[]) {  
           
         EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
          EntityManager em = emf.createEntityManager();  
          em.getTransaction().begin( );  
            
          CriteriaBuilder cb=em.getCriteriaBuilder();  
            
          AbstractQuery<StudentEntity> cq=cb.createQuery(StudentEntity.class);  
          
         Root<StudentEntity> stud=cq.from(StudentEntity.class);  
           
          
         cq.where(cb.in(stud.get("s_age")).value(22).value(24));  
          CriteriaQuery<StudentEntity> select = ((CriteriaQuery<StudentEntity>) cq).select(stud);  
          TypedQuery<StudentEntity> tq = em.createQuery(select);  
          List<StudentEntity> list = tq.getResultList();  
            
        System.out.println("Students having age 22 and 24");  
            
          System.out.print("s_id");  
             System.out.print("\t s_name");  
             System.out.println("\t s_age");  
            
          for(StudentEntity s:list)  
          {  
          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: