java 我什么时候应该在 JPA 中使用 @JoinColumn 或 @JoinTable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30288464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When Should I Use @JoinColumn or @JoinTable with JPA?
提问by Sarah Szabo
@JoinColumn
gives an Entity
a foreign key to another Entity
whereas @JoinTable
will list the relationship between all relationships between Entity
A and Entity
B. As far as I can tell, they both appear to do similar things. When should I use one or the other?
@JoinColumn
给Entity
另一个外键,Entity
而@JoinTable
将列出Entity
A 和Entity
B之间所有关系之间的关系。据我所知,它们似乎都在做类似的事情。我什么时候应该使用其中一种?
回答by Vlad Mihalcea
Let's say you have an entity A
which has a @ManyToOne
association ot an entity B
假设您有一个与实体A
有@ManyToOne
关联的实体B
@JoinColumnwill define the target table Foreign Key (e.g B_ID
) while using the target Entity table (e.g. B
).
@JoinColumn将B_ID
在使用目标实体表(例如B
)时定义目标表外键(例如)。
@Entity
public class A {
private Long id;
@ManyToOne
@JoinColumn(name="B_ID")
private B b;
}
@JoinTablewill use a separate table to hold the relationship between A
and B
.
@JoinTable将使用一个单独的表来保存之间的关系A
和B
。
@Entity
public class A {
private Long id;
@ManyToOne
@JoinTable(
name = "A_B",
joinColumns = @JoinColumn(name = "B_ID"),
inverseJoinColumns = @JoinColumn(name = "A_ID")
)
private B b;
}
This time neither A
nor B
contain any Foreign Key, because there's a separate table (e.g. A_B
) to hold the association between A
and B
.
这一次没有A
,也不B
含有任何外键,因为有一个单独的表(例如A_B
)持有之间的关联A
和B
。
回答by Rabin Pantha
@JoinTable stores the id of both the table into a separate table while @JoinColumn stores id of the another table in a new column.
@JoinTable 将两个表的 id 存储到一个单独的表中,而 @JoinColumn 将另一个表的 id 存储在一个新列中。
@JoinTable : This is the default type. Use this when you you need a more normalized database. ie. to reduce redundancy.
@JoinTable :这是默认类型。当您需要更规范化的数据库时使用此选项。IE。以减少冗余。
@JoinColumn : Use this for better performance as it does not need to join extra table.
@JoinColumn :使用它可以获得更好的性能,因为它不需要加入额外的表。
回答by mhrsalehi
one important difference: @JoinColumn
always depends upon the contextit is used:
一个重要的区别:@JoinColumn
总是 取决于它使用的上下文:
- If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy, the foreign key column is in the table of the
source entity or embeddable.- If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity.
- If the join is for a ManyToMany mapping or for a OneToOne or bidirectional ManyToOne/OneToMany mapping using a join table, the
foreign key is in a join table.- If the join is for an element collection, the foreign key is in a collection table.
- 如果连接用于使用外键映射策略的 OneToOne 或 ManyToOne 映射,则外键列位于
源实体的表中或可嵌入。- 如果联接用于使用外键映射策略的单向 OneToMany 映射,则外键位于目标实体的表中。
- 如果连接用于多对多映射或使用连接表的单对一或双向多对一/一对多映射,则
外键位于连接表中。- 如果联接用于元素集合,则外键在集合表中。