I know this has been discussed many times. I have Googled, I have searched the forums, but so far no one seems to have a satisfactory solution to this problem.
To summarize the issue, if I have:
Code:
public class A {
@OneToMany(fetchType=FetchType.EAGER)
private Set<B> bs;
}
public class B {
private String prop;
}
Let's say I have one instance of A, and that instance of A has 4 B instances in it's collection, then if I do:
Code:
Criteria crit = session.createCriteria(A.class);
crit.createAlias("bs", "b", Criteria.LEFT_JOIN).add(Restrictions.eq("b.prop", ...);
List results = crit.list();
where the Restriction will match all four B instances, 'results' will have 4 objects in it, when it should have one.
The most often proposed solutions is, pass the List to a Set, where if you've implemented hashCode/equals properly, then, the Set will filter out dups.
The other solution is to call:
Code:
crit.setResultsTransformer(Criteria.DISTINCT_ROOT_ENTITY);
The problem with both of the above solutions is that neither work correctly with paging and I have yet to see an acceptable solution.
What IS the solution here? Is there a good one?
Thanks,
Justin