Java 什么是@JoinColumn 以及它在 Hibernate 中的使用方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37542208/
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
what is @JoinColumn and how it is used in Hibernate
提问by zbryan
I have been reading a lot about @JoinColumn but I still don't get the idea behind it.
我已经阅读了很多关于@JoinColumn 的文章,但我仍然不明白它背后的想法。
Patient Table
病人表
CREATE TABLE patient (
patient_id BIGINT NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
PRIMARY KEY(patient_id));
Vehicle Table
车辆表
CREATE TABLE vehicles (
patient_id BIGINT NOT NULL,
vehicle_id BIGINT NOT NULL,
vehicle_manufacturer VARCHAR(255),
PRIMARY KEY (vehicle_id),
CONSTRAINT patienthasmanyvehicle FOREIGN KEY(patient_id) REFERENCES patient(patient_id));
Patient Class
病人班
@OneToMany(mappedBy = "patient")
private Collection<Vehicle> patientVehicles = new ArrayList<Vehicle>();
Vehicle Class
车辆等级
@ManyToOne
@JoinColumn(name="patient_id")
private Patient patient;
I'm confused on how the Vehicle class part, what is the relationship between
我对 Vehicle 类的部分感到困惑,它们之间的关系是什么
Vehicle Class ---- Entity
@JoinColumn(name="patient_id") ---- annotation
private Patient patient ----field
Does it say; The Vehicle Entityhas a Foreign Keyto Patient entitynamed patient_id. Add the patient_idas a column in the Vehicle Entity table
有没有说;该车辆实体有一个外键到患者实体命名patient_id。在Vehicle Entity 表中添加patient_id作为一列
Do the name parameter of the JoinColumn should always be a Foreign Key or Primary Key?
JoinColumn 的 name 参数应该始终是外键还是主键?
I have been reading this but I'm still confuse. JPA JoinColumn vs mappedBy
我一直在读这个,但我仍然很困惑。 JPA JoinColumn 与 mappingBy
回答by Vishal nigam
Vehicle Class ---- Entity @JoinColumn(name="patient_id") ---- annotation private Patient patient ----field
Vehicle Class ---- Entity @JoinColumn(name="patient_id") ---- 注释私有的Patient 病人----field
Above code will generate a column patient_id (a foreign key) in Vehicle class which will point to Patient Class primary key.
上面的代码将在 Vehicle 类中生成一个列patient_id(外键),该列将指向 Patient Class 主键。
MappedBy - This attribute tells us that this relation will be managed by Vehicle class. Example. If we insert a vehicle, then two SQL will be injected if cascadetype is all/save. 1st SQL will inject details in Patient table and 2nd SQL will inject vehicle details in vehicle table with patient_id column of Vehicle column pointing to Patient tuple inserted.
MappedBy - 这个属性告诉我们这个关系将由 Vehicle 类管理。例子。如果我们插入一个车辆,那么如果cascadetype是all/save,就会注入两条SQL。第一个 SQL 将在 Patient 表中注入详细信息,第二个 SQL 将在 Vehicle 表中注入车辆详细信息,其中 Vehicle 列的patient_id 列指向插入的 Patient 元组。
回答by v.ladynev
A unidirectional association via a join table
通过连接表的单向关联
@Entity
class Patient {
@OneToMany
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
}
A bidirectional association via a join table
通过连接表的双向关联
@Entity
class Patient {
@OneToMany
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
@ManyToOne(fetch = FetchType.LAZY)
private Patient patient;
}
A unidirectional association via a foreign key
通过外键的单向关联
@Entity
class Patient {
@OneToMany
@JoinColumn
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
}
A bidirectional association via a foreign key
通过外键的双向关联
@Entity
class Patient {
@OneToMany(mappedBy = "patient")
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
@ManyToOne(fetch = FetchType.LAZY)
private Patient patient;
}
A bidirectional association via a foreign key with a foreign column name specification
通过具有外列名称规范的外键的双向关联
@Entity
class Patient {
@OneToMany(mappedBy = "patient")
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="patient_id")
private Patient patient;
}
This is the basic starting point of using @JoinColumn
.
这是使用@JoinColumn
.
To verify that the foreign key(patient_id
in the Vehicle
table) is really mapped in the patients table you can use @JoinColumn(nullable = false)
要验证的外键(patient_id
在Vehicle
表)中可以使用患者台真正映射@JoinColumn(nullable = false)
@Entity
class Vehicle {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="patient_id", nullable = false)
private Patient patient
}
回答by Randy Orton
Why is that the
patient_id
(generated column which is a FK) in the Vehicle Table doesn't have any value when I run my code?
为什么
patient_id
当我运行我的代码时,车辆表中的(生成的 FK 列)没有任何值?
All @JoinColumn
does is to specify a column for joining an entity association or element collection. Since you have made @JoinColumn
associated with Patient class object, that's why foreign key is created on Patient table.
所有@JoinColumn
所做的就是为加盟实体协会或元素集合指定列。由于您已@JoinColumn
与 Patient 类对象关联,这就是在 Patient 表上创建外键的原因。
For more please refer https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/JoinColumn.html
更多请参考https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/JoinColumn.html
回答by Lova Chittumuri
The join column is declared with the @JoinColumn annotation which looks like the @Column annotation. It has one more parameters named referencedColumnName. This parameter declares the column in the targeted entity that will be used to the join.
连接列是用@JoinColumn 注释声明的,它看起来像@Column 注释。它还有一个名为referencedColumnName 的参数。此参数声明目标实体中将用于连接的列。
In a bidirectional relationship, one of the sides (and only one) has to be the owner: the owner is responsible for the association column(s) update. To declare a side as not responsible for the relationship, the attribute mappedBy is used. mappedBy refers to the property name of the association on the owner side.
在双向关系中,其中一侧(并且只有一侧)必须是所有者:所有者负责关联列的更新。为了声明不负责关系的一方,使用了属性mappedBy。mappingBy 指的是所有者侧关联的属性名称。
Here is Sample code :
这是示例代码:
EntityOne :
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TEST_ID")
private EntityTwo entityTwo;
EntityTwo :
// bi-directional many-to-one association to EntityOne Here TEST_ID is the Primary key
@OneToMany(mappedBy = "entityTwo")
private List<EntityOne> entityOne;
回答by Yogesh Sanchihar
The table in which join column will be found depends upon context.
将在其中找到连接列的表取决于上下文。
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 映射,则外键位于目标实体的表中。
如果联接用于多对多映射或使用联接表的单对一或双向多对一/一对多映射,则外键在联接表中。
如果联接用于元素集合,则外键在集合表中。