休眠:为什么要为类似天气的字段绑定使用显式@JoinColum批注? [英] Hibernate: Why is binding for a field similar weather explicit @JoinColum annotation is used or not?

查看:82
本文介绍了休眠:为什么要为类似天气的字段绑定使用显式@JoinColum批注?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用任何关联类型注释(例如@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 @OneToManyis 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

推荐答案

当我们使用@ 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.

当您指定@JoinColumnmappedBy(甚至没有@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批注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆