JPA Predicate criteriabuilder example for beginners

JPA Predicate criteriabuilder example for beginners

Predicate is an interface which extends Expression and can be used for conjunction or disjunction of restrictions.

 

Syntax to create a predicate:

Predicate someCondition = criteriaBuilder.equal(affiliateUrlRoot.get("shop_home_url"), "mirthbees");

 

 

criteriaBuilder is a CriteriaBuilder, which can be created like this,

 

@PersistenceContext
private EntityManager entityManager;
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

affiliateUrlRoot is the root of your entity model [shop_home_url – is your field name inside the pojo/model class], which can be created,

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

 

“mirthbees” is the value which is going to get assigned to the field “shop_home_url” with equal condition.

 

You can create an arraylist of type Predicate to store all your predicate conditions.

final List predicates = new ArrayList();
predicates.add(someCondition);

 

Now you can use where class to execute your predicate(from predicates list added above) conditions and return the result,

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

 

AffiliateUrl => is the model which has the below fields,


private String affiliate_network_name;
Sample value: MIRTHBEES
private String shop_home_url;
sample url: www.mirthbees.com

 

Sample hibernate created query below for your reference:

Hibernate: select affiliateu0_.affiliate_url_id as affiliat1_0_, affiliateu0_.affiliate_network_name as affiliat2_0_, affiliateu0_.affiliate_shop_identity as affiliat3_0_, affiliateu0_.created_date_time as created_4_0_, affiliateu0_.created_user_id as created_5_0_, affiliateu0_.last_updated_date_time as last_upd6_0_, affiliateu0_.last_updated_user_id as last_upd7_0_, affiliateu0_.shop_home_affiliate_url as shop_hom8_0_, affiliateu0_.shop_home_url as shop_hom9_0_ from affiliate_url affiliateu0_

where
(affiliateu0_.shop_home_url like ?)

and
(affiliateu0_.affiliate_network_name in (?))

 

For better easy understanding, I am sharing the complete source code with category model with predicate:


@Override
public List searchCategoriesByParms(Category category) {
//searchCategoriesByParms

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Category.class);
Root categoryRoot = criteriaQuery.from(Category.class);
List allPredicates = new ArrayList();

if(category.getCategoryName()!=null && !category.getCategoryImage().isEmpty()){
Predicate categoryNmLikePredicate = criteriaBuilder.like(categoryRoot.get("categoryName"), "%"+category.getCategoryName()+"%");
allPredicates.add(categoryNmLikePredicate);
}

criteriaQuery.select(categoryRoot).where(allPredicates.toArray(new Predicate[allPredicates.size()]));
List matchedCategoriesList = entityManager.createQuery(criteriaQuery).getResultList();

return matchedCategoriesList;
}

 

 

 

Feel free to let me know if you have any queries in the below comments section.

221 total views, 1 views today

Leave a Reply

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