Q:

HQL that returns nested children where all the children matches the condition

I have a class Parent with a one-to-many children set. The children is nested and can have n number as depth. I want to write a hql which will return a parent with children, grandchildren, great-grandchildren etc that matches a condition. 
I was able to get children that matches the condition, but when i call, children.getChildren, it returns grand children which do not matches the condition. Is this possibile?
Thanks,
Parvathy

A:

There are 2 ways to achieve this:

When you fetch Parent class' object, lazily load its child objects by specifying lazy = false either in the mapping file or at Entity level. As far as you condition is concerned, you can write the logic in your programming language (e.g. loop over the child objects).
Write a recursive logic, that makes database hit till the last nth grandchild is fetched. It cannot be accomplished with single criteria or HQL.

