如何在JPA中拥有相同类型的2个集合? [英] How to have 2 collections of the same type in JPA?
问题描述
我在JPA中有2个实体:条目和注释。 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>();
...
}
JPA + Hibernate创建Entry表,注释表和SINGLEEntry_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,主键(Entry_id,dnr),unique(descriptionComments_id),unique(postMortemComments_id))
存储对象失败,因为 descriptionComments_id
和 postMortemComments_id
不能为not null
如何使用JPA + Hibernate存储包含两个相同类型集合的对象?
这是许多Hibernate的bug之一( HHH-3410 )。
我已经设法通过添加 @JoinTable
注释到 @OneToMany
关系,每个都有自己的表名。
在您的情况下,它将如下所示:
@OneToMany (fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinTable(name =entity_descriptioncomments)
@IndexColumn(base = 1,name =dnr)
private List<评论> 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>();注意:必须添加 @IndexColumn
$ b 注释(因为有多个EAGER包的其他Hibernate问题: HHH -1718 / EJB-346 )。
I've got 2 entities in JPA: Entry and Comment. Entry contains two collections of Comment objects.
@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>();
...
}
To store such objects, JPA+Hibernate creates "Entry" table, "Comment" table and 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))
Storing of objects fail as descriptionComments_id
and postMortemComments_id
cannot be "not null" at the same time.
How do I store object containing two collections of the same type using JPA+Hibernate?
This is one of the many Hibernate bugs (HHH-3410 to be precise).
I've managed to fix it by adding @JoinTable
annotations to @OneToMany
relationships, each having its own table name.
In your case it would look like this:
@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>();
Note: you must add @IndexColumn
annotation as well (because of the other Hibernate issue with multiple EAGER bags: HHH-1718/EJB-346).
这篇关于如何在JPA中拥有相同类型的2个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!