This tutorial will show you how to use JPA(Java Persistence API) Criteria Queries for selecting data from database.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

The Criteria API is used to define queries for entities and their persistent state by creating query-defining objects. Criteria queries are written using Java programming language APIs, are typesafe, and are portable. Such queries work regardless of the underlying data store. The major advantage of Criteria API is that errors can be detected earlier during the compile time. String-based JPQL(Java Persistence Query Language) queries and JPA criteria based queries are same in performance and efficiency.

Steps to create Criteria Query

1. EntityManager instance is used to create a CriteriaBuilder object.
2. CriteriaQuery instance is used to create a query object from CriteriaBuilder object. The attributes of this object will be modified with the details of the query.
3. CriteriaQuery.form method is called to set the Root of the query.
4. is called to set the result list type for a particular POJO class mapped to a database table.
5. TypedQuery<T> instance is used to prepare a query for execution and specifying the type of the query result.
6. getResultList method is used on the TypedQuery<T> object for executing a query to return the result list of a collection of entities.

The following example shows you how to use JPA Criteria Queries in an application.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.


The following configurations are required in order to run the application

Eclipse Mars
JDK 1.8
Have maven installed and configured
JPA dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse.

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : jpa

Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""




		<!-- JPA -->

		<!-- mysql java connector -->


Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/main/resources folder for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/main/resources and click on Finish button.

Then create META-INF folder under src/main/resources source directory.

Step 5. Create an XML file persistence.xml under src/main/resources/META-INF.

Do right-click on META-INF in the project and go New -> file. Give File name: as persistence.xml and click on Finish button.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns=""
	<persistence-unit name="userPersistanceUnit"
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cdcol" />
			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="javax.persistence.jdbc.password" value="" />

			<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
			<property name="hibernate.transaction.flush_before_completion"
				value="true" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />

In the above XML file, we have transaction-type as RESOURCE_LOCAL. There are two types of Transaction management types supported in JPA.

JTA or GLOBAL Transactions

Resource local transactions refer to the native transactions of the JDBC Driver whereas JTA transactions refer to the transactions of the JEE server. A Resource Local transaction involves a single transactional resource, for example a JDBC Connection. Whenever you need two or more resources( for example a JMS Connection and a JDBC Connection )  within a single transaction, you use  JTA Transaction. Container Managed Entity Managers always use JTA transactions as the container takes care of transaction life cycle management and spawning the transaction across multiple transactional resources. Application Managed Entity Managers can use either Resource Local Transactions or JTA transactions.

Normally in JTA or global transaction, a third party transaction monitor enlists the different transactional resources within a transaction, prepares them for a commit and finally commits the transaction. This process of first preparing the resources for transaction(by doing a dry run) and then committing(or rolling back) is called a 2 phase commit.

Step 6. Create below entity class UserDetails under src/main/java folder

package com.roytuts.jpa.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "user_details")
public class UserDetails {

	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private int id;

	@Column(name = "first_name")
	private String firstName;

	@Column(name = "last_name")
	private String lastName;

	@Column(name = "email")
	private String email;

	@Column(name = "dob")
	private String dob;

	public UserDetails() {

	public UserDetails(int id, String firstName, String lastName, String email, String dob) { = id;
		this.firstName = firstName;
		this.lastName = lastName; = email;
		this.dob = dob;

	public int getId() {
		return id;

	public void setId(int id) { = id;

	public String getFirstName() {
		return firstName;

	public void setFirstName(String firstName) {
		this.firstName = firstName;

	public String getLastName() {
		return lastName;

	public void setLastName(String lastName) {
		this.lastName = lastName;

	public String getEmail() {
		return email;

	public void setEmail(String email) { = email;

	public String getDob() {
		return dob;

	public void setDob(String dob) {
		this.dob = dob;

	public String toString() {
		return "UserDetails [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
				+ ", dob=" + dob + "]";


Step 7. Create below custom exception class JpaException under src/main/java folder

package com.roytuts.jpa.exception;

public class JpaException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public JpaException(String msg) {

	public JpaException(Throwable t) {

	public JpaException(String msg, Throwable t) {
		super(msg, t);


Step 8. Now create an enum PersistenceManager for creating EntityManagerFactory and EntityManager

package com.roytuts.jpa.manager;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public enum PersistenceManager {


	private EntityManagerFactory emf;

	private PersistenceManager() {
		emf = Persistence.createEntityManagerFactory("userPersistanceUnit");

	public EntityManager getEntityManager() {
		return emf.createEntityManager();

	public void close() {


Step 9. Create a main class which will test the application.

package com.roytuts.jpa.crud.test;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.roytuts.jpa.entity.UserDetails;
import com.roytuts.jpa.exception.JpaException;
import com.roytuts.jpa.manager.PersistenceManager;

public class JpaCriteriaTest {

	public static void main(String[] args) {
		EntityManager em = PersistenceManager._INSTANCE.getEntityManager();

		try {

			CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
			CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();
			Root<UserDetails> from = criteriaQuery.from(UserDetails.class);

			// select all records
			System.out.println("Select all records");
			CriteriaQuery<Object> select =;
			TypedQuery<Object> typedQuery = em.createQuery(select);
			List<Object> resultlist = typedQuery.getResultList();

			for (Object o : resultlist) {
				UserDetails u = (UserDetails) o;
				System.out.println("ID : " + u.getId() + " First Name : " + u.getFirstName() + " Last Name : "
						+ u.getLastName() + " Email : " + u.getEmail() + " DOB : " + u.getDob());

			// Ordering the records
			System.out.println("Select all records in ascending order");
			CriteriaQuery<Object> select1 =;
			TypedQuery<Object> typedQuery1 = em.createQuery(select);
			List<Object> resultlist1 = typedQuery1.getResultList();
			for (Object o : resultlist1) {
				UserDetails u = (UserDetails) o;
				System.out.println("ID : " + u.getId() + " First Name : " + u.getFirstName() + " Last Name : "
						+ u.getLastName() + " Email : " + u.getEmail() + " DOB : " + u.getDob());

		} catch (JpaException e) {
		} finally {

		// update user for id 11
		try {
			UserDetails user = new UserDetails();
		} catch (Exception e) {


Step 11. Create table user_details in MySQL database and dump some data into it

USE `cdcol`;

/*Table structure for table `user_details` */

DROP TABLE IF EXISTS `user_details`;

CREATE TABLE `user_details` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `last_name` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `dob` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
insert  into 

Step 12. Run the above main class, you will see below output in the console.

Select all records

    select as id1_6_,
        userdetail0_.dob as dob2_6_, as email3_6_,
        userdetail0_.first_name as first_na4_6_,
        userdetail0_.last_name as last_nam5_6_ 
        user_details userdetail0_
ID : 7 First Name : Soumitra Last Name : Roy Email : DOB : 30-08-2016
ID : 8 First Name : Souvik Last Name : Sanyal Email : DOB : 30-09-1991
ID : 13 First Name : Liton Last Name : Sarkar Email : DOB : 14-07-1990

Select all records in ascending order

    select as id1_6_,
        userdetail0_.dob as dob2_6_, as email3_6_,
        userdetail0_.first_name as first_na4_6_,
        userdetail0_.last_name as last_nam5_6_ 
        user_details userdetail0_ 
    order by
        userdetail0_.first_name asc
ID : 13 First Name : Liton Last Name : Sarkar Email : DOB : 14-07-1990
ID : 7 First Name : Soumitra Last Name : Roy Email : DOB : 30-08-2016
ID : 8 First Name : Souvik Last Name : Sanyal Email : DOB : 30-09-1991

Thanks for reading.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

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