简体   繁体   English

Java,休眠瞬态字段

[英]Java, hibernate transient field

Im having domain object Item with fields id, name, parentItem and category(Boolean). 我有域对象Item,字段ID,名称,parentItem和category(Boolean)。 So im inserting values in database like: 所以我在数据库中插入值,例如:

id name parentItem category id名称parentItem类别
1 Tools NULL 1 1工具NULL 1
2 Electric tools 1 1 2电动工具1 1
3 Small tools 2 1 3小型工具2 1
4 Actual tool end child item 3 0 4实际工具结束子项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. 所以我需要通过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). 例如:如果用户在dataTable过滤器“ Electric”中输入,则需要返回“ Electric tools”类别中的所有项目以及子类别中的所有项目(在此示例中为Small tools,如果存在则列出所有其他项目)。

So currently i have in java domain object @Transient field which uses recursion to get items path. 所以目前我在java域对象@Transient字段中使用递归来获取项目路径。 But i cant search by Transient fields. 但是我无法通过瞬态字​​段进行搜索。 I mean i cant implement search in database because this field is: 我的意思是我无法在数据库中实现搜索,因为此字段是:
1. Transient 1.瞬态
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. 2.使用递归,如果我需要在其他数据库版本上部署应用程序,我将不得不在数据库或其他内容上重写递归sql。 I dont like this 我不喜欢这样

Can anyone point me to some clever, unique solution ? 谁能指出我一些聪明的独特解决方案? Any idea, advice is appreciated. 任何想法,建议表示赞赏。 Thanks! 谢谢!

This question looks very similar: HQL recursion, how do I do this? 这个问题看起来非常相似: HQL递归,我该怎么做?

In short: You cannot do recursion in HQL. 简而言之:您不能在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) 编写一个本机查询来执行此操作(是的,由于递归查询不是标准的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) (节省内存,但是很多数据库工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM