Question

Im having domain object Item with fields id, name, parentItem and category(Boolean). So im inserting values in database like:

id name parentItem category
1 Tools NULL 1
2 Electric tools 1 1
3 Small tools 2 1
4 Actual tool end child item 3 0

so actual item is in category/path "Tools/Electric tools/Small tools"

So i need to implements lazy filter search by categoryName/categoryPath. For example: if user inputs in dataTable filter "Electric" i need to return all items in "Electric tools" category and all items from subcategorys ( in this example Small tools and all other if they exist).

So currently i have in java domain object @Transient field which uses recursion to get items path. But i cant search by Transient fields. I mean i cant implement search in database because this field is:
1. Transient
2. Uses recursion and if i need to deploy app on other db version, i will have to rewrite recursion sql on db or something. I dont like this

Can anyone point me to some clever, unique solution ? Any idea, advice is appreciated. Thanks!

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top