Vil*_*kas 15 java collections persistence hibernate jpa
我在JPA中有两个实体:Entry和Comment.Entry包含两个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>();
...
}
Run Code Online (Sandbox Code Playgroud)
为了存储这样的对象,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存储包含两个相同类型集合的对象?
Ido*_*lon 13
这是许多Hibernate错误之一(准确地说是HHH-3410).
我已经设法通过向关系添加@JoinTable注释来修复它@OneToMany,每个关系都有自己的表名.
在你的情况下,它看起来像这样:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name="entity_descriptioncomments")
@IndexColumn(base = 1, name = "dnr")
private List<Comment> descriptionComments = new ArrayList<Comment>();
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name="entity_postmortemcomments")
@IndexColumn(base = 1, name = "pmnr")
private List<Comment> postMortemComments = new ArrayList<Comment>();
Run Code Online (Sandbox Code Playgroud)
注意:您还必须添加@IndexColumn注释(因为多个EAGER包的其他Hibernate问题:HHH-1718/EJB-346).
| 归档时间: |
|
| 查看次数: |
6813 次 |
| 最近记录: |