Hibernate CollectionOfElements EAGER 获取重复元素 [英] Hibernate CollectionOfElements EAGER fetch duplicates elements

查看:23
本文介绍了Hibernate CollectionOfElements EAGER 获取重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 SynonymMapping 的类,它有一个映射为 CollectionOfElements 的值的集合

I have a class called SynonymMapping which has a collection of values mapped as a CollectionOfElements

@Entity(name = "synonymmapping")
public class SynonymMapping {

    @Id private String keyId;

    //@CollectionOfElements(fetch = FetchType.EAGER)
    @CollectionOfElements
    @JoinTable(name="synonymmappingvalues", joinColumns={@JoinColumn(name="keyId")})
    @Column(name="value", nullable=false)
    @Sort(type=SortType.NATURAL)
    private SortedSet<String> values;

    public SynonymMapping() {
        values = new TreeSet<String>();
    }

    public SynonymMapping(String key, SortedSet<String> values) {
        this();
        this.keyId = key;
        this.values = values;
    }

    public String getKeyId() {
        return keyId;
    }

    public Set<String> getValues() {
        return values;
    }
}

我有一个测试,我将两个 SynonymMapping 对象存储到数据库,然后要求数据库返回所有保存的 SynonymMapping 对象,期望收到我存储的两个对象.

I have a test where I store two SynonymMapping objects to the database and then ask the database to return all saved SynonymMapping objects, expecting to receive the two objects I stored.

当我将值的映射更改为急切(如代码中注释掉的行所示)并再次运行测试时,我收到四个匹配项.

When I change the mapping of values to be eager (as shown in in the code by the commented out line) and run the test again, I receive four matches.

我已经在两次运行之间清除了数据库,我可以在急切和懒惰之间交换这个问题.

I have cleared out the database between runs and I can duplicate this problem swapping between eager and lazy.

我认为这与 hibernate 在下面创建的连接有关,但我在网上找不到明确的答案.

I think it has to do with the joins that hibernate creates underneath but I can't find a definite answer online.

谁能告诉我为什么急切提取会复制对象?

Can anyone tell me why an eager fetch is duplicating the objects?

谢谢.

推荐答案

在映射中强制执行预先获取通常不是一个好主意 - 最好在适当的查询中指定预先连接(除非您 100% 确定在没有填充该集合的情况下,您的对象在任何和所有情况下都没有意义/有效).

It's generally not a good idea to enforce eager fetching in the mapping - it's better to specify eager joins in appropriate queries (unless you're 100% sure that under any and all circumstances your object won't make sense / be valid without that collection being populated).

你得到重复的原因是因为 Hibernate 在内部连接了你的根表和集合表.请注意,它们确实是重复的,例如对于具有 3 个集合元素的 2 个 SynonymMapping,您将获得 6 个结果 (2x3),每个 SynonymMapping 实体的 3 个副本.因此,最简单的解决方法是将结果包装在 Set 中,从而确保它们是唯一的.

The reason you're getting duplicates is because Hibernate internally joins your root and collection tables. Note that they really are duplicates, e.g. for 2 SynonymMappings with 3 collection elements each you would get 6 results (2x3), 3 copies of each SynonymMapping entity. So the easiest workaround is to wrap results in a Set thereby ensuring they're unique.

这篇关于Hibernate CollectionOfElements EAGER 获取重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆