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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:49:57  来源:igfitidea点击:

When Should I Use @JoinColumn or @JoinTable with JPA?

javahibernatejpaormhibernate-mapping

提问by Sarah Szabo

@JoinColumngives an Entitya foreign key to another Entitywhereas @JoinTablewill list the relationship between all relationships between EntityA and EntityB. As far as I can tell, they both appear to do similar things. When should I use one or the other?

@JoinColumnEntity另一个外键,Entity@JoinTable将列出EntityA 和EntityB之间所有关系之间的关系。据我所知,它们似乎都在做类似的事情。我什么时候应该使用其中一种?

回答by Vlad Mihalcea

Let's say you have an entity Awhich has a @ManyToOneassociation 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).

@JoinColumnB_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 Aand B.

@JoinTable将使用一个单独的表来保存之间的关系AB

@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 Anor Bcontain any Foreign Key, because there's a separate table (e.g. A_B) to hold the association between Aand B.

这一次没有A,也不B含有任何外键,因为有一个单独的表(例如A_B)持有之间的关联AB

回答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: @JoinColumnalways 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 映射,则外键位于目标实体的表中。
  • 如果连接用于多对多映射或使用连接表的单对一或双向多对一/一对多映射,则
    外键位于连接表中。
  • 如果联接用于元素集合,则外键在集合表中。