The SELECT clause is used to fetch the data from database. The data can be retrieved in the form of single expression or multiple expressions. In Criteria API, each form is expressed differently.
Criteria SELECT Example
Generally, select() method is used for the SELECT clause to fetch all type of forms. Here, we will perform several SELECT 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. Here, we created 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
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-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. Selecting Single Expression
Here, we will fetch single column from database with the help of a simple example.
SingleFetch.java
import com.javahubpoint.jpa.StudentEntity;
import javax.persistence.*;
import javax.persistence.criteria.*;
import java.util.*;
public class SingleFetch {
public static void main( String args[]) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );
EntityManager em = emf.createEntityManager();
em.getTransaction().begin( );
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery<StudentEntity> cq=cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud=cq.from(StudentEntity.class);
cq.select(stud.get("s_name"));
CriteriaQuery<StudentEntity> select = cq.select(stud);
TypedQuery<StudentEntity> q = em.createQuery(select);
List<StudentEntity> list = q.getResultList();
System.out.println("s_id");
for(StudentEntity s:list)
{
System.out.println(s.getS_id());
}
em.getTransaction().commit();
em.close();
emf.close();
}
}
Output:
2. Selecting Multiple Expression
Here, we will fetch multiple columns from database with the help of a simple example.
MultiFetch.java
import com.javahubpoint.jpa.StudentEntity;
import javax.persistence.*;
import javax.persistence.criteria.*;
import java.util.*;
public class MultiFetch {
public static void main( String args[]) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );
EntityManager em = emf.createEntityManager();
em.getTransaction().begin( );
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery<StudentEntity> cq=cb.createQuery(StudentEntity.class);
Root<StudentEntity> stud=cq.from(StudentEntity.class);
cq.multiselect(stud.get("s_id"),stud.get("s_name"),stud.get("s_age") );
CriteriaQuery<StudentEntity> select = cq.select(stud);
TypedQuery<StudentEntity> q = em.createQuery(select);
List<StudentEntity> list = q.getResultList();
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: