JPA CriteriaBuilder example

In this post, we will see about the JPA CriteriaBuilder example. We will see how to retrieve data from the database using JPA CriteriaBuilder. In this tutorial first, we will see important methods of CriteriaBuilder(for example equal(), gt(), like() etc.) and generated query, later we will have a complete Spring boot JPA CriteriaBuilder example from scratch.

The CriteriaBuilder contains predefined methods that used to define queries to fetch the records/entities. Let’s see how JPA Criteria is different from Spring Data JPA and JPQL.

Consider we have entity called Student.java as below.



@Entity
public class Student {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

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

	@Column(name = "roll_number")
	private String rollNumber;

}

We want to fetch all students on basis of name.

Using Spring Data JPA.

public List<Student> findByName(String name); 

See complete example here.

Using Spring data JPQL.

@Query("select s from Student s where s.name = ?1")
List<Student> findStudents(String name);

See complete example here.

Using Spring Data native query.

@Query(value = "select * from Student s where s.name = ?1", nativeQuery = true)
List<Student> findStudentsUsingNativeQuery(String name);

See complete example here.

Using JPA CriteriaBuilder.

criteriaQuery.where(criteriaBuilder.equal(studentRoot.get("name), name ));

Steps to retrieve data from database using JPA CriteriaBuilder.

Step 1 – Create CriteriaBuilder from entityMananger. See more details about JPA EntityManager here.

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

Step 2 – Create CriteriaQuery from CriteriaBuilder.

CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Student.class);

Step 3 – Get Root object using criteriaQuery.

Root studentRoot = criteriaQuery.from(Student.class);

Step 4 – Call select() method with CriteriaQuery.

criteriaQuery.select(studentRoot);

Step 5 – Get TypedQuery object.

TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);

Step 6 – Get the List of Entites.

List studentList = typedQuery.getResultList();

Retrieving multiple records/entities using JPA CriteriaBuilder

@Transactional
public List<Student> getStudents() {    

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList;
}

Generated Query for above example.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_ 
    from
        student student0_

Retrieving Single record/entity using JPA CriteriaBuilder

We need to follow similar steps for the previous example. Additionally, we need to add where condition using the where() method to the CriteriaQuery.

criteriaQuery.where(criteriaBuilder.equal(studentRoot.get(“id”), id ));

@Transactional
public Student getStudent(Long id) {
    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    criteriaQuery.where(criteriaBuilder.equal(studentRoot.get("id"), id ));
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList.get(0);
}

Generated Query for above code snippet.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_ 
    from
        student student0_ 
    where
        student0_.id=1

Note – same way we can retrieve the entity on basis of othere filed. for example we can retrive can entity on basis of name.

criteriaQuery.where(criteriaBuilder.equal(studentRoot.get("name"), name ));

JPA CriteriaBuilder equal example.

Predicate predicate = criteriaBuilder.equal(studentRoot.get(“id”), id);

@Transactional
public List<Student> getStudent(Long id) {        
    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicate = criteriaBuilder.equal(studentRoot.get("id"), id);
    criteriaQuery.where(predicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList;
}
Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.id=1

JPA CriteriaBuilder notequal example.

@Transactional
public List<Student> getStudent(Long id) {        
    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicate = criteriaBuilder.notEqual(studentRoot.get("id"), id);
    criteriaQuery.where(predicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList;
}
Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.id!=1

JPA CriteriaBuilder or Example.

We can add the OR condition using Predicate. For example, we want to fetch data on the basis of name or rollNumber.

@Transactional
public List<Student> getStudents(String name, String rollNumber) {

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicateName = criteriaBuilder.equal(studentRoot.get("name"), name);
    Predicate predicateRollNumber = criteriaBuilder.equal(studentRoot.get("rollNumber"), rollNumber);
    Predicate nameOrRollNumberPredicate = criteriaBuilder.or(predicateName, predicateRollNumber);
    criteriaQuery.where(nameOrRollNumberPredicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();

    return studentList;
}

Query generated for above example.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_ 
    from
        student student0_ 
    where
        student0_.name=? 
        or student0_.roll_number=?

JPA CriteriaBuilder and example.

Example would be same as previous one. We need to use and() method instead of or() method.

@Transactional
public List<Student> getStudents(String name, String rollNumber) {  

    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicateName = criteriaBuilder.equal(studentRoot.get("name"), name);
    Predicate predicateRollNumber = criteriaBuilder.equal(studentRoot.get("rollNumber"), rollNumber);
    Predicate nameOrRollNumberPredicate = criteriaBuilder.and(predicateName, predicateRollNumber);
    criteriaQuery.where(nameOrRollNumberPredicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();

    return studentList;
}

Query generated for above example.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_ 
    from
        student student0_ 
    where
        student0_.name=? 
        and student0_.roll_number=?

JPA CriteriaBuilder not example.

@Transactional
public List<Student> getStudents(String name) { 
    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicateName = criteriaBuilder.equal(studentRoot.get("name"), name);
    Predicate lastPredicate = criteriaBuilder.not(predicateName);
    criteriaQuery.where(lastPredicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList;
}

Query generated in above case.

	Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.name<>?

JPA CriteriaBuilder gt(greater than) Example.

Let’s say, we want to retrive all data which id is greater than 2.

Predicate greaterRestriction = criteriaBuilder.gt(studentRoot.get(“id”).as(Long.class), id);

@Transactional
public List<Student> getStudents(Long id) { 
    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate lastPredicate = criteriaBuilder.gt(studentRoot.get("id").as(Long.class), id);
    criteriaQuery.where(lastPredicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList;
}
Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.id>2

JPA CriteriaBuilder lt(less than) Example.

Let’s say, we want to retrive all data which id is less than 2.

Predicate lessThanRestriction = criteriaBuilder.lt(studentRoot.get(“id”).as(Long.class), id);

@Transactional
public List<Student> getStudents(Long id) { 
    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate lastPredicate = criteriaBuilder.lt(studentRoot.get("id").as(Long.class), id);
    criteriaQuery.where(lastPredicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList;
}
Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.id<2

JPA CriteriaBuilder ge(greater than equal to) Example.

Predicate lessThanRestriction = criteriaBuilder.ge(studentRoot.get(“id”).as(Long.class), id);

Query Generated.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.id>=2

JPA CriteriaBuilder le(less than equal to) Example.

Predicate lessThanRestriction = criteriaBuilder.le(studentRoot.get(“id”).as(Long.class), id);

@Transactional
public List<Student> getStudents(Long id) {

    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicate = criteriaBuilder.le(studentRoot.get("id"), id);
    criteriaQuery.where(predicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();

    return studentList;
}

Query Generated.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.id<=2

JPA CriteriaBuilder between Example.

Retrieve all records from database which id is between 2 and 5.

Predicate lastPredicate = criteriaBuilder.between(studentRoot.get(“id”).as(Long.class),id1,id2);

In above code snippet id1 = 2 and id2 = 5.

@Transactional
public List<Student> getStudents(Long id1, Long id2) {

    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate lastPredicate = criteriaBuilder.between(studentRoot.get("id").as(Long.class),id1,id2);
    criteriaQuery.where(lastPredicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();
    return studentList;
}

Query Generated.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.id between 2 and 5

JPA CriteriaBuilder like Example

First JPA Builder like example -like ‘r’

Predicate predicate = criteriaBuilder.like(studentRoot.get(“name”), “r”);

@Transactional
public List<Student> getStudents() {


    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicate = criteriaBuilder.like(studentRoot.get("name"), "r");
    criteriaQuery.where(predicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();

    return studentList;
}

Query generated.

    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.name like ?
2020-07-26 11:22:13.089 TRACE 16676 --- [nio-9091-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [r]

Second JPA CriteriaBuilder like example – like ‘%r%’

Predicate predicate = criteriaBuilder.like(studentRoot.get(“name”), “%r%”);

@Transactional
public List<Student> getStudents() {

    CriteriaBuilder  criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);      
    Root<Student> studentRoot = criteriaQuery.from(Student.class);
    criteriaQuery.select(studentRoot);
    Predicate predicate = criteriaBuilder.like(studentRoot.get("name"), "%r%");
    criteriaQuery.where(predicate);
    TypedQuery<Student> typedQuery = entityManager.createQuery(criteriaQuery);
    List<Student> studentList = typedQuery.getResultList();

    return studentList;
}

Query Generated.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.name like ?
2020-07-26 11:22:13.089 TRACE 16676 --- [nio-9091-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [%r%]

Fourth JPA CriteriaBuilder like example – like ‘r%’

Predicate predicate = criteriaBuilder.like(studentRoot.get(“name”), “r%”);

Query Generated.

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    where
        student0_.name like ?
2020-07-26 11:22:13.089 TRACE 16676 --- [nio-9091-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [r%]

Sorting using JPA CriteriaBuilder – criteriaBuilder.asc() method

Sort Student on basis of name ascending order.

@Transactional
public List<Student> getStudents() {

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);
    Root<Student> studentRoot = criteriaQuery.from(Student.class);

    criteriaQuery.orderBy(criteriaBuilder.asc(studentRoot.get("name")));
    List<Student> students = entityManager.createQuery(criteriaQuery).getResultList();
    return students;
}

Query Generated

Hibernate: 
    select
        student0_.id as id1_0_,
        student0_.name as name2_0_,
        student0_.roll_number as roll_num3_0_,
        student0_.university as universi4_0_ 
    from
        student student0_ 
    order by
        student0_.name asc

Sort Student on basis of name descending order.

To sort in descending order, we need to use criteriaBuilder.desc() method.

JPA CriteriaBuilder Example – Select only specific columns

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);
// Create the Root
Root<Student> root = criteriaQuery.from(Student.class);
criteriaQuery.multiselect(root.get(Student.ID), root.get(Student.NAME));  //using metamodel
List<Student> students= em.createQuery(criteriaQuery).getResultList();
for (Student student : students) {
    Long id = (Long) student.get(0);
    Long name = (Long) students.get(1);
}

Spring Boot JPA CriteriaBuilder example

Create a maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – hibernatecriteriaexample, ArtifactId – hibernatecriteriaexampleand name – hibernatecriteriaexample) and click on finish. Keep packaging as the jar.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>hibernatecriteriaexample</groupId>
	<artifactId>hibernatecriteriaexample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hibernatecriteriaexample</name>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
	</dependencies>
</project>

Directory structure

JPA CriteriaBuilder example directory structure

Student.java

package com.javatute.entity;

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

@Entity
public class Student {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

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

	@Column(name = "roll_number")
	private String rollNumber;

	@Column(name = "university")
	private String university;
	
//getter & setter

}

StudentService.java

package com.javatute.service;

import java.util.List;

import org.springframework.stereotype.Component;

import com.javatute.entity.Student;

@Component
public interface StudentService {

	public Student save(Student student);

	public List<Student> getStudents();
}

Note – Check what is difference between @Component, @Service, @Repository and @Controller annotation here.

StudentServiceImpl.java

package com.javatute.serviceimpl;

import java.util.List;

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

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.javatute.entity.Student;
import com.javatute.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public Student save(Student student) {
		entityManager.persist(student);
		return student;
	}

	@Transactional
	public List<Student> getStudents() {

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);
		Root<Student> studentRoot = criteriaQuery.from(Student.class);
		criteriaQuery.orderBy(criteriaBuilder.desc(studentRoot.get("name")));
		List<Student> students = entityManager.createQuery(criteriaQuery).getResultList();
		return students;
	}

}

See more about @Transactional annotation here. First, we are getting JPA EntityManager and later using that entityManager we are getting CriteriaBuilder object. See more details about JPA EntityManger here.

StudentController.java

package com.javatute.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javatute.entity.Student;
import com.javatute.service.StudentService;

@RestController
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentService studentService;

	@PostMapping("/create")
	public Student createStudent(@RequestBody Student student) {
		Student createResponse = studentService.save(student);
		return createResponse;
	}
	
	@GetMapping("/allstudents")
	public List<Student> getStudents() {
		List<Student> getReponse = studentService.getStudents();
		return getReponse;
	}
}

See more about @RestController and @Controller annotation here. We have two rest end point, first one use to create new student and another one is use to retrive students.

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrudexample
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
server.port = 9091
logging.level.org.hibernate.type.descriptor.sql=trace

SpringMain.java

package com.javatute.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = "com.javatute.*")
@EntityScan("com.javatute.*")
public class SpringMain {
	public static void main(String[] args) {
		SpringApplication.run(SpringMain.class, args);
	}
}

See more about @ComponentScan annotation here.

Spring Boot JPA CriteriaBuilder Example

http://localhost:9091/student/create

Request data.

{

    "name": "kim",
    "rollNumber": "0126CS01",
    "university": "rgtu"
}

http://localhost:9091/student/allstudents

JPA CriteriaBuilder example

That’s all about JPA CriteriaBuilder example.

Download the source code from github.

See other JPA examples.

Spring Data JPA and Hibernate tutorials.

JPA CriteriaBuilder docs.

Summry – We have seen how to retrieve data using JPA CriteriaBuilder and also covered important methods of CriteriaBuilder.