Java 具有两个以上OneToMany字段的休眠实体
我有以下hibernate实体:Java 具有两个以上OneToMany字段的休眠实体,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,我有以下hibernate实体: @Entity public class Customer { @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List<Address> addresses = new ArrayList<>(); @OneToMany(mappedBy = "customer", ca
@Entity
public class Customer {
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Address> addresses = new ArrayList<>();
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Contact> contacts = new ArrayList<>();
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Name> names = new ArrayList<>();
// Many more, including a primary key
}
如果我删除了一个任意OneToMany
关联,或者如果我向一个任意OneToMany
关联添加了一个@Fetch(value=FetchMode.JOIN)
,一切正常
这是一个hibernate错误,一个hibernate限制,还是我的实体有什么问题?蒂亚 这不是一个bug。这是因为Hibernate使用带有连接的one select来获取所有数据。Hibernate可以连接三个或更多的表,但是连接的结果将有重复的,例如,
Address
列。Hibernate需要删除重复项-这是Set
工作的原因之一
可能的解决办法:
- 使用
Set 尝试使用:
@OneToMany @LazyCollection(value=LazyCollectionOption.TRUE) private Collection<Name> names = new ArrayList<>();
如果没有hibernate,我可以连接两个以上的表。hibernate不能连接三个表,这是一个限制吗?它适用于我使用的@OneToMany @LazyCollection(值=LazyCollectionOption.TRUE) 私有集合名称=new ArrayList();
或FetchType.LAZY
,但速度可能较慢。使用FetchMode.SUBSELECT
而不是集合
不起作用。列表
@OneToMany @LazyCollection(value=LazyCollectionOption.TRUE) private Collection<Name> names = new ArrayList<>();