Spring Boot with CriteriaBuilder for Dynamic Search Queries

Spring Boot with CriteriaBuilder for Dynamic Search Queries:

Spring Boot with CriteriaBuilder for Dynamic Search Queries:
When you are developing an enterprise application, you have to provide a search option dynamically with “and” condition. Our grand father way to do is using stringbuilder to construct the query in java and return the result.
But hibernate has provided criteria for the same with better performance. But in spring boot you can use JPA to get the better results.

In your service class you can use persistencecontext to get the entitymanager, which is needed to get the criteriaBuilder.



public class MyServiceClassImpl implements MyServiceInterface {


private EntityManager em;



public List<AffiliateUrl> searchByParams(AffiliateUrl affiliateUrl) {

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();

CriteriaQuery<AffiliateUrl> criteriaQuery = criteriaBuilder.createQuery(AffiliateUrl.class); Root<AffiliateUrl> affiliateUrlRoot = criteriaQuery.from(AffiliateUrl.class); criteriaQuery.select(affiliateUrlRoot);

final List<Predicate> predicates = new ArrayList<Predicate>();

if (affiliateUrl.getShop_home_url() != null && !affiliateUrl.getShop_home_url().isEmpty()) {

predicates.add(criteriaBuilder.and(criteriaBuilder.equal(affiliateUrlRoot.get("shop_home_url"), affiliateUrl.getShop_home_url())));


if (affiliateUrl.getAffiliate_network_name() != null && !affiliateUrl.getAffiliate_network_name().isEmpty()) { predicates.add(criteriaBuilder.and(criteriaBuilder.equal(affiliateUrlRoot.get("affiliate_network_name"), affiliateUrl.getAffiliate_network_name())));



criteriaQuery.select(affiliateUrlRoot)criteriaQuery.select(affiliateUrlRoot) .where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]))); List<AffiliateUrl> results = em.createQuery(criteriaQuery).getResultList();

return results;





What Am I doing here ?

  • Get EntityManager using PersistenceContext annotation.
  • Get CriteriaBuilder Object using the entitymanager [em.getCriteriaBuilder()].
  • Get Root of your entity class using criteriaQuery from class.
  • Now criteriaQuery have your entity, we have to write a predicates with and/or/like etc to get the required results.
  • Have a check and add your criteria with and/or/whatever you need to predicates list.
  • Finally input the predicates list constructed to criteriaBuilder.
  • Use createQuery method with the constructed criteriaQuery to get the result in List<AffiliateUrl>.


here get(“affiliate_network_name”) should match with the member name of the AffiliateUrl Entity.


MyServiceInterface.java will looks like :

public interface MyServiceInterface {

public List<AffiliateUrl> searchByParams(AffiliateUrl affiliateUrl);



AffiliateUrl is my entity class here, I am going to fetch the result from this class only through JPA.

The above snippets will function this way:

1. Basically I have two field named shop_home_url and affiliate_network_name

2. If I have given the values for both the field then it will construct with “and” and returns the result which matches both the criteria.

3. If I input anyone then the other condition skipped and results returned accordingly.

4. I have tested it for my requirement and it works fine.



JPA predicate can be used for “in” condition like this,


Expression<String> exp = affiliateUrlRoot.get("affiliate_network_name");

Predicate predicate = exp.in(tempNetworkNmLstSelected); criteriaQuery.where(predicate);



predicates has the list of predicate, where everything can be given to criteriabuilder like this and get it executed:


criteriaQuery.select(affiliateUrlRoot) .where(criteriaBuilder.and(<span style="text-decoration: underline;"><strong>predicates</strong></span>.toArray(new Predicate[predicates.size()])));


HereĀ tempNetworkNmLstSelected is a list of strings which can be passed to the in values, something like (select * from tempTable where id in (X) etc..)


Feel free to write your query in the below comments section to help you better.



822 total views, 1 views today

Leave a Reply

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