【发布时间】:2010-10-14 18:22:11
【问题描述】:
我在 JPA 中有 2 个实体:Entry 和 Comment。条目包含两个 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”表和 SINGLE“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))
对象存储失败,因为descriptionComments_id 和postMortemComments_id 不能同时为“非空”。
如何使用 JPA+Hibernate 存储包含两个相同类型集合的对象?
【问题讨论】:
标签: java hibernate jpa collections persistence