Consider the following scenario:
public class Parent {
private List<Child> childs;
}
public class Child {
private Target target;
private Value value;
}
public class Target {
private Long id;
}
public class Value {
private Long id;
}
For every determined Target-object Parent-object will have Child-object that has Value-object.
I get filter in format of HashMap<Long>, List<Long>>
where key is Target-object's id and value is list of desired Value-object id's.
What I need to get with JPA Criteria API is for example all Parents with following conditions:
- has child with targetId 1 with valueId in(1,2,3)
- AND has child with targetId 2 with valueId in(1,4)
So I need to exlude the results that dosen't meet all the requirements. Is there a way to achieve this with JPA Criteria API. I have tried the following:
List<Predicate> predicates = new ArrayList<>();
...
List<Predicate> subPredicates = new ArrayList<>();
Path<Child> child = root.join(Parent_.child, JoinType.LEFT);
for (Map.Entry<Long, List<Long>> entry : map.entrySet()) {
Long targetId = entry.getKey();
List<Long> valueIds = entry.getValue();
subPredicates.add(cb.and(cb.equal(child.get(Child_.target), targetId), child.get(Child_.value).in(valueIds)));
}
predicates.add(cb.and(subPredicates.toArray(new Predicate[subPredicates.size()])));
But obviously it doesn't work. Is this even possible with this kind of class structure?. Can anyone help me with this?