如何在JPA中拥有2个相同类型的集合?
我在JPA中有2个实体:条目和评论。条目包含两个 Comment 对象集合。
@Entity
public class Entry {
...
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@IndexColumn(base = 1, name = "dnr")
private List<Comment> descriptionComments = new ArrayList<Comment>();
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@IndexColumn(base = 1, name = "pmnr")
private List<Comment> postMortemComments = new ArrayList<Comment>();
...
}
为了存储这样的对象,JPA+Hibernate创建了“Entry”表,“Comment”表和单个“Entry_Comment”:
create table Entry_Comment (Entry_id integer not null, postMortemComments_id integer not null, pmnr integer not null, descriptionComments_id integer not null, dnr integer not null, primary key (Entry_id, dnr), unique (descriptionComments_id), unique (postMortemComments_id))
对象的存储失败,因为不能同时“不为 null”。descriptionComments_id
postMortemComments_id
如何使用 JPA+休眠来存储包含两个相同类型集合的对象?