Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Criteria Queries in Spring Boot Data Jpa Application

I have an application that uses Spring Boot Data jpa . So far i am using a repository like this

public interface StudentRepository extends CrudRepository<StudentEntity, Integer>{
    @Query(value = "" 
        + "SELECT s.studentname "
        + "FROM   studententity s, "
        + "       courseentity c "
        + "WHERE  s.courseid = c.courseid "
        + "       AND s.courseid IN (SELECT c.courseid "
        + "                          FROM   courseentity c "
        + "                          WHERE  c.coursename = ?1)")
    List<String> nameByCourse(String coursename);
}

How can i make use of Criteria Query that Hibernate provides for such cases in a Spring Boot Application

like image 750
Rahul Avatar asked May 31 '17 07:05

Rahul


People also ask

What is Criteria query in JPA?

The Criteria API is a predefined API used to define queries for entities. It is the alternative way of defining a JPQL query. These queries are type-safe, and portable and easy to modify by changing the syntax.

What is the use of JpaSpecificationExecutor?

The JpaSpecificationExecutor<T> interface declares the methods that can be used to invoke database queries that use the JPA Criteria API. This interface has one type parameter T that describes the type of the queried entity.


2 Answers

From the docs

To enrich a repository with custom functionality you first define an interface and an implementation for the custom functionality. Use the repository interface you provided to extend the custom interface.

Define an interface like so

public interface StudentRepositoryCustom {

    List<String> nameByCourse(String coursename);

}

Then define a custom implementation of this interface like so

@Service
class StudentRepositoryImpl implements StudentRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    public List<String> nameByCourse(String coursename) {            
        CriteriaBuilder cb = em.getCriteriaBuilder();
        //Using criteria builder you can build your criteria queries.
    }

}

Now you can extend this custom repository implementaion in your JPA repository like so.

public interface StudentRepository extends CrudRepository<StudentEntity, Integer>, StudentRepositoryCustom {

}

Learn more about criteria query and criteria builder here

like image 105
Abdullah Khan Avatar answered Nov 11 '22 06:11

Abdullah Khan


With Spring-boot-jpa you are able to use entityManager nearly everywhere. The most commom way is to create an own interface for custom methods.

public interface StudentCustomRepository {

    void anyCustomMethod();
    Student getStudentByName(String name);
}

Then implement this interface to a service class where you are able to autowire and use the entityManager:

@Service
public class StudentCustomRepositoryServiceImpl implements StudentCustomRepository {

     @PersistenceContext
     private EntityManager em;

     @Override
     public void anyCustomMethod(){
         //here use the entityManager
     }

     @Override
     StudentEntity getStudentByName(String name){
         Criteria crit = em.unwrap(Session.class).createCriteria(StudentEntity.class);
         crit.add(Restrictions.eq("name", name));
         List<StudentEntity> students = crit.list();
         return students.get(0);
     }
 }

You can also decide to implement your StudentRepository to your new StudentCustomRepositoryServiceImpl class.

like image 45
Patrick Avatar answered Nov 11 '22 04:11

Patrick