休眠查询不返回完整对象 [英] Hibernate query not returning full object
问题描述
我有一个物品清单.每个项目都有一组类别.我想抓取特定类别的所有物品.这部分很简单.我遇到问题的部分是让我的查询返回包含所有类别的商品,而不仅仅是我要过滤的商品.
I have a list of items. Each item has set of categories. I want to grab all the items of a specific category. This part is simple. The part I am having problems with is getting my query to return the item with all of its categories, not just the one I am filtering on.
session.createCriteria(Item.class)
.createAlias("categories","category")
.add(Restrictions.eq("category.name",categoryFilter))
上面的代码返回该项目,但仅包含我正在过滤的类别.无论如何,要说在此限制条件下过滤对象,但返回完整的对象而不是已过滤的对象?我也尝试过用HQL编写此程序,但结果相同.
The above code returns the item but only with the category I am filtering on. Is there anyway to say filter the object on this restriction, but return the full object and not the filtered one? I have also tried writing this in HQL with the same results.
推荐答案
似乎FetchMode与createAlias之间确实存在一些令人讨厌的交互,对我来说似乎是个错误.
It appears there really is some nasty interaction between FetchMode and createAlias, which looks like a bug to me.
在 https://forums.hibernate.org/viewtopic中对此进行了一些讨论..php?t = 944439 ,其中一位开发人员强调说这是正确的行为,不会得到解决.
There's some discussion of this at https://forums.hibernate.org/viewtopic.php?t=944439 with one of the developers saying emphatically that it's correct behavior and won't be fixed.
讨论中还包含潜在的解决方法.
The discussion also contains potential workarounds though.
尝试使用嵌套条件而不是别名:
Try using a nested criteria instead of an alias:
session.createCriteria(Item.class)
.createCriteria("categories")
.add(Restrictions.eq("name",categoryFilter))
将集合映射为渴望的对象,这似乎对我有用.不确定是否可以在外部条件上使用FetchMode进行交互.
With the collection mapped as eager, this seems to work for me. Not sure of interaction with using FetchMode on the outer criteria.
这篇关于休眠查询不返回完整对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!