I have an entity which has a relationship with two child collections, as shown here:
Parent
+-- Child (collection)
+-- GrandChild (collection)
What I want to have returned is the list of distinct parent entities where the grand child entity contains a property of a specific value. So long as only one of the grandchildren of a specific parent contain the value, I get a single hit; however as soon as multiple grandchildren contain this value, I get multiple parent entities returned.
Code:
Criteria parentCriteria = getSession().createCriteria(Parent.class);
parentCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Criteria childCriteria = parentCriteria.createCriteria("children");
Criteria grandChildCriteria = childCriteria.creteCriteria("children");
grandChildCriteria.add(Restrictions.eq("propertyA", "valueA"));
Is there a way to work around this where I can still use the Criteria API to restrict my parent entities as desired but without having the parent returned multiple times when the grand child record has multiple matches?