Custom use of JpaRepository in Spring boot

Keywords: Java Spring SQL Database

Generic JpaRepository class

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Long>{
}

General filter queries of JpaRepository can be solved in the form of findBy, which is easy to use in general, but there are always some business queries that cannot be solved by JpaRepository. At this time, you need to add a custom Repository class on this basis.

Custom Repository class

Interface class

import java.util.List;
public interface EmployeeRepositoryCustom {
    List<Employee> getFirstNamesLike(String firstName);
}

Implementation class

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Repository
public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom {
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public List<Employee> getFirstNamesLike(String firstName) {
        Query query = entityManager.createNativeQuery("SELECT em.* FROM spring_data_jpa_example.employee as em " +
                "WHERE em.firstname LIKE ?", Employee.class);
        query.setParameter(1, firstName + "%");
        return query.getResultList();
    }
}

**Note: * * the name of the implementation class must be the name of the implementation class bean + RepositoryImpl here; the EntityManager is injected here. For its standard query, I will use it later.

Use the custom class Repository class

@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Long>, EmployeeRepositoryCustom {
}

It is still used in the way of JpaRepository, as long as it is simply inherited.

Standard query for EntityManager

If you don't want to involve too many sql strings in java code, you should query the database according to the standard query method of EntityManager, as follows:

@Repository
public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom {
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public List<Employee> getFirstNamesLike(String firstName) {
        // Query query = entityManager.createNativeQuery("SELECT em.* FROM spring_data_jpa_example.employee as em " +
        //         "WHERE em.firstname LIKE ?", Employee.class);
        // query.setParameter(1, firstName + "%");
		// return query.getResultList();
		
		CriteriaBuilder builder = entityManager.getCriteriaBuilder();
		CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
		Root<Employee> root = query.from(Employee.class);
		Predicate predicate = builder.conjunction();

		predicate = builder.and(predicate,  builder.like(root.get("firstname"), "Zhang San"));
		query.where(predicate);

		return entityManager.createQuery(query).getResultList();
    }
}

Reference resources:

REST Query Language with Spring and JPA Criteria 1. Working with Spring Data Repositories Add Custom Functionality to a Spring Data Repository

Posted by Nicholas Reed on Sat, 04 Jan 2020 01:42:54 -0800