I have Cat and Kitten entities, that are many-to-one relationship (Cat has many kittens)
I need to return those Cats and only those Kittens of that Cat that matches certain conditions.
I.e.
Code:
from Cat cat
left join fetch cat .kittens kitten
where kitten.color = 'gray'
Using this hql i ran into the following problem:
If one Cat has N 'gray' kittens, I got N instances of the same Cat returned with N kittens pre-loaded in Cat.kittens collection.
If I will use 'left join' w/o 'fetch' I got correct number of Cats, but Cat.kittens now contains all kittens, including 'gray'.
The 'distinct' keyword doesnt work for me too.
How i can load entity with pre-loaded 'many-to-one' assosiation that matches criteria?