I'm trying to find all entities that have some nested elements and nested elemens have collections of elements, and I need to find it by property of those collections.
It would be something like this
class A{
private B b;
}
class B{
private Collection<C> cCol;
}
class C{
private String name;
}
So I want to get all A elements that have B elements that have a C which name matches given parameter.
Not sure how to do it with JPA Critieria API. I know there is in predicate, or MEMEBER OF in JPQL but I need to search by property of element in collection, not a collection member.
Tried things like root.get(a.b.c.name)
and also with root.fetch(a.b)
or root.fetch(b.c)
but always ended up with some exceptions about illegal api usage
aQuery.distinct(true).where(cb.equal(root.get("b").get("cCol").get("name"), searchString))
should also give the same results without the need for a subquery - the equivalent JPQL query would beSELECT DISTINCT a from A a JOIN a.b b JOIN b.cCol c WHERE c.name = :searchString
– Watchword