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:

[java]
Predicate someCondition = criteriaBuilder.equal(affiliateUrlRoot.get(“shop_home_url”), “mirthbees”);
[/java]

 

 

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

 

[java]
@PersistenceContext
private EntityManager entityManager;
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
[/java]

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

[java]
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(AffiliateUrl.class);
Root affiliateUrlRoot = criteriaQuery.from(AffiliateUrl.class);
[/java]

 

“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.
[java]
final List predicates = new ArrayList();
predicates.add(someCondition);
[/java]

 

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

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

 

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

[plain]

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

[/plain]

 

Sample hibernate created query below for your reference:

[plain]
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 (?))
[/plain]

 

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

[java]

@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;
}

[/java]

 

 

 

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

Leave a Reply