This question looks very similar: HQL recursion, how do I do this?
In short: You cannot do recursion in HQL. Your best bets are:
- Write a native query to do this (and yes, you would have to rewrite it with every database move, since recursive queries are not standard SQL)
- Use a join column to have parents/children in the object and traverse&filter the product tree in memory (uses more memory since you preload everything, but only hits DB once)
- Make multiple queries if you know your tree is not too deep. (Saves memory, but a lot of database work)