问题描述
当我们使用任何关联类型注释(例如@OneToOne
,@OneToMany
等)时,是@JoinColum
隐式指定的.
Is @JoinColum
implicitly specified when we use any association type annotation like @OneToOne
, @OneToMany
, etc.
以下是与Laptop
实体相关联的Student
实体的代码段
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
@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)那么我可以假设在指定@OneToMany
时隐式指定了@JoinColum
吗?但是等等,我还注意到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?
调试:private List<Laptop> laptops=new ArrayList<Laptop>();
//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
推荐答案
答案是:否.除了使用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>();
这篇关于休眠:为什么要为类似天气的字段绑定使用显式@JoinColum批注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!