Hibernate:为什么绑定字段类似天气显式@JoinColum 注释是否使用? [英] Hibernate: Why is binding for a field similar weather explicit @JoinColum annotation is used or not?
问题描述
@JoinColum
是否在我们使用任何关联类型注释(如@OneToOne
、@OneToMany
等)时隐式指定.
Is @JoinColum
implicitly specified when we use any association type annotation like @OneToOne
, @OneToMany
, etc.
这是来自 Student
实体与 Laptop
实体的片段
Here is snippet from Student
entity in association with Laptop
entity
案例 1) 不使用显式 @JoinColum
Case 1) Without using explicit @JoinColum
@OneToMany(cascade=CascadeType.ALL)
private List<Laptop> laptops=new ArrayList<Laptop>();
案例 2) 使用显式 @JoinColum
Case 2) Using explicit @JoinColum
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn
private List<Laptop> laptops=new ArrayList<Laptop>();
当我检查 DEBUG
时,两种情况都有几乎"相似的 Binding
,即如下所示.
When I check DEBUG
both cases have "almost" similar Binding
, i.e something like below.
1) 那么我可以假设 @JoinColum
是在指定 @OneToMany
时隐式指定的吗?但是等等,我还注意到 Hibernate 试图引用一些我没有在任何地方创建的完全奇怪的表student_laptops"(可能是连接表).
1) So could I assume @JoinColum
is implicitly specified when @OneToMany
is specified? But wait I also noticed Hibernate is trying to refer to some totally strange table 'student_laptops' which I have not created anywhere (probably a join table).
那么有人可以描述一下这里的流程是什么吗?
So can someone describe whats exactly the flow going here?
Debug: private List
//You can totally ignore this. Just specifying for any reference.
DEBUG - Binding column: Ejb3JoinColumn{logicalColumnName='null', referencedColumn='null', mappedBy=''}
DEBUG - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(STUDENT), mappingColumn=laptops, insertable=true, updatable=true, unique=false}
DEBUG - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(STUDENT), mappingColumn=null, insertable=true, updatable=true, unique=false}
DEBUG - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(STUDENT), mappingColumn=element, insertable=true, updatable=true, unique=false}
DEBUG - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(STUDENT), mappingColumn=laptops_KEY, insertable=true, updatable=true, unique=false}
DEBUG - Binding column: Ejb3JoinColumn{logicalColumnName='laptops_KEY', referencedColumn='null', mappedBy='null'}
DEBUG - Binding column: Ejb3JoinColumn{logicalColumnName='null', referencedColumn='null', mappedBy=''}
DEBUG - Binding column: Ejb3JoinColumn{logicalColumnName='null', referencedColumn='null', mappedBy=''}
DEBUG - Collection role: com.infiniteskills.data.entities.Student.laptops
DEBUG - Building property laptops
推荐答案
当我们使用@OneToOne、@OneToMany 等任何关联类型注解时,@JoinColum 是否隐式指定
Is @JoinColum implicitly specified when we use any association type annotation like @OneToOne, @OneToMany, etc.
答案是:否.使用 mappedBy
的情况除外.
The answer is: NO. Except the case when mappedBy
is used.
当您指定 @JoinColumn
或 mappedBy
(即使没有 @JoinColumn
)Hibernate 意味着您希望通过外键使用关联(没有连接表).
When you specify @JoinColumn
or mappedBy
(even without @JoinColumn
) Hibernate implies that you want to use an association via a foreign key (without a join table).
当您不指定 @JoinColumn
时,Hibernate 使用连接表并为您创建它 - student_laptops
在您的情况下.
When you don't specify @JoinColumn
Hibernate uses a join table and creates it for you — student_laptops
in your case.
您可以添加 @JoinTable
注释以强调您使用的是 jon 表这一事实.
You can add a @JoinTable
annotation to stress the fact that you are using a jon table.
@OneToMany(cascade = CascadeType.ALL)
@JoinTable
private List<Laptop> laptops = new ArrayList<Laptop>();
这篇关于Hibernate:为什么绑定字段类似天气显式@JoinColum 注释是否使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!