I am facing an issue for JPA criteria query. How can I add multiple where clause in my Criteria Query with if else...
My Requirement is:
CriteriaBuilder builder = getEm().getCriteriaBuilder();
CriteriaQuery<Object> query = builder.createQuery(Object.class);
// From
Root<EntityClass> entity= query.from(EntityClass.class);
// Select
query.multiselect(entity.get("id").get("col1"),entity.get("id").get("col2"));
// Where
Predicate p1 = builder.and(builder.equal(entity.get("col3"), value3));
Predicate p2 = builder.and(builder.equal(entity.get("col4"), value4));
Predicate p3 = builder.and(builder.equal(entity.get("col5"), value5));
if(someCondition1){
query.where(p1);
}else if(someCondition2){
query.where(p1);
}
query.where(p3);
In above code the statement query.where(p3); replaces previously set where clause condition p1 and p2. What the alternate I found is conjunct like below
if(someCondition1){
query.where(p1, p3);
}else if(someCondition2){
query.where(p2, p3);
}else{
query.where(p3);
}
But that is not a good approach, because when there are many if-else this becomes very bad to write repeating codes. Can any one have a solution for this?
You can create a Predicate array to store your conditional Predicates and then add it to the where clause:
List<Predicate> predList = new LinkedList<Predicate>();
if(someCondition1){
predList.add(p1);
}else if(someCondition2){
predList.add(p2);
}
predList.add(p3);
Predicate[] predArray = new Predicate[predList.size()];
predList.toArray(predArray);
query.where(predArray);
This allows to dynamically add any kind of predicate.