java Hibernate list() 返回重复项是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1560239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it valid for Hibernate list() to return duplicates?
提问by skaffman
Is anyone aware of the validity of Hibernate's Criteria.list()and Query.list()methods returning multiple occurrences of the same entity?
有没有人知道 Hibernate 的有效性Criteria.list()和Query.list()返回同一实体多次出现的方法的有效性?
Occasionally I find when using the Criteria API, that changing the default fetch strategy in my class mapping definition (from "select" to "join") can sometimes affect how many references to the same entity can appear in the resulting output of list(), and I'm unsure whether to treat this as a bug or not. The javadoc does not define it, it simply says "The list of matched query results." (thanks guys).
有时,我发现在使用 Criteria API 时,更改我的类映射定义中的默认获取策略(从“选择”到“加入”)有时会影响在结果输出中出现对同一实体的引用数量list(),并且我我不确定是否将此视为错误。javadoc 没有定义它,它只是说“匹配的查询结果列表”。(多谢你们)。
If this is expected and normal behaviour, then I can de-dup the list myself, that's not a problem, but if it's a bug, then I would prefer to avoid it, rather than de-dup the results and try to ignore it.
如果这是预期的和正常的行为,那么我可以自己删除列表,这不是问题,但如果它是一个错误,那么我宁愿避免它,而不是删除结果并尝试忽略它。
Anyone got any experience of this?
有人有这方面的经验吗?
采纳答案by Eemeli Kantola
Yes, getting duplicates is perfectly possible if you construct your queries so that this can happen. See for example Hibernate CollectionOfElements EAGER fetch duplicates elements
是的,如果您构建查询以便发生这种情况,则完全有可能获得重复项。参见例如Hibernate CollectionOfElements EAGER fetch duplicates elements
回答by Sam Berry
I also started noticing this behavior in my Java API as it started to grow. Glad there is an easy way to prevent it. Out of practice I've started out appending:
随着 Java API 的发展,我也开始注意到这种行为。很高兴有一种简单的方法可以防止它。出于实践,我开始附加:
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
To all of my criteria that return a list. For example:
对于我所有返回列表的标准。例如:
List<PaymentTypeAccountEntity> paymentTypeAccounts = criteria()
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
回答by Mike
If you have an object which has a list of sub objects on it, and your criteria joins the two tables together, you could potentially get duplicates of the main object.
如果您有一个包含子对象列表的对象,并且您的条件将两个表连接在一起,则可能会获得主对象的重复项。
One way to ensure that you don't get duplicates is to use a DistinctRootEntityResultTransformer. The main drawback to this is if you are using result set buffering/row counting. The two don't work together.
确保不会出现重复的一种方法是使用 DistinctRootEntityResultTransformer。这样做的主要缺点是如果您使用结果集缓冲/行计数。两者不合作。
回答by hecko84
I had the exact same issue with Criteria API. The simple solution for me was to set distinct to true on the query like
我在 Criteria API 上遇到了完全相同的问题。对我来说,简单的解决方案是在查询中将 distinct 设置为 true
CriteriaQuery<Foo> query = criteriaBuilder.createQuery(Foo.class);
query.distinct(true);
Another possible option that came to my mind before would be to simply pass the resulting list to a Set which will also by definition have just an object's single instance.
我之前想到的另一个可能的选择是简单地将结果列表传递给一个 Set,根据定义,它也只有一个对象的单个实例。

