|
To whoever falls into this situation and can't just give up on the Criteria API:
-
Use INNER_JOIN wherever possible instead of LEFT_OUTER_JOIN if joining on a collection.
-
If LEFT_OUTER_JOIN must be used on collection, be sure to have a (dummy) restriction on the collection, otherwise it may get loaded with inconsistent elements. A restriction blocks the collection from being initialized with the results of the sub query. The collection will then be loaded as expected when used by the code (lazy initialization).
-
Example of "dummy" restriction: Restrictions.isNotEmpty( collection ). Rationale: if you're left-joining on it to get to a child element and filter on it, the matched collection can't be empty anyway.
|