什么是@JoinColumn,以及它如何在Hibernate中使用 [英] what is @JoinColumn and how it is used in Hibernate
问题描述
患者表
CREATE TABLE patient(
patient_id BIGINT NOT NULL,
first_name VARCHAR(255)NOT NULL,
last_name VARCHAR(255)NOT NULL,
PRIMARY KEY(patient_id));
车辆表
CREATE TABLE车辆(
patient_id BIGINT不为NULL,
vehicle_id BIGINT不为NULL,
vehicle_manufacturer VARCHAR(255),
PRIMARY KEY(vehicle_id),
CONSTRAINT patienthasmanyvehicle FOREIGN KEY(patient_id)参考患者(patient_id));
病人分类
@OneToMany(mappedBy =patient)
private Collection< Vehicle> patientVehicles = new ArrayList< Vehicle>();
车辆级别
@ManyToOne
@JoinColumn(name =patient_id)
私人患者;
我很困惑Vehicle类的部分,
车辆类别----实体
@JoinColumn(name =patient_id)----注释
私人患者患者----现场
是否说; 车辆实体具有名为 patient_id 的外键至患者实体。
将 patient_id 添加为车辆实体表中的一列
JoinColumn应始终是一个外键或主键?
我一直在阅读这篇文章,但我仍然很迷惑。
JPA JoinColumn vs mappedBy
通过连接表进行关联
@Entity
class病人{
@OneToMany
私人收藏< Vehicle> patientVehicles = new ArrayList< Vehicle>();
}
@ Entity
class Vehicle {
}
$ b 通过外键进行单向关联
@Entity
class Patient {
@OneToMany
@JoinColumn(name =patient_id)
private Collection< Vehicle> patientVehicles = new ArrayList< Vehicle>();
}
@ Entity
class Vehicle {
}
$ b 通过外键进行双向关联
@Entity
。
类Patient {
@OneToMany(mappedBy =patient)
private Collection< Vehicle> patientVehicles = new ArrayList< Vehicle>();
}
@ Entity
class车辆{
@JoinColumn(name =patient_id)
私人患者患者
$ b这是使用<$ c $的基本出发点c> @JoinColumn
验证外键(
patient_id
车辆
表)确实映射到您可以使用的患者表中@JoinColumn(nullable = false)
@Entity
class Vehicle {
@JoinColumn(name =patient_id,nullable = false)
私人病人
}
I have been reading a lot about @JoinColumn but I still don't get the idea behind it.
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 Class ---- Entity @JoinColumn(name="patient_id") ---- annotation private Patient patient ----field
Does it say; The Vehicle Entity has a Foreign Key to Patient entity named patient_id. Add the patient_id as a column in the Vehicle Entity table
Do the name parameter of the JoinColumn should always be a Foreign Key or Primary Key?
I have been reading this but I'm still confuse. JPA JoinColumn vs mappedBy
解决方案An association via a join table
@Entity class Patient { @OneToMany private Collection<Vehicle> patientVehicles = new ArrayList<Vehicle>(); } @Entity class Vehicle { }
A unidirectional association via a foreign key
@Entity class Patient { @OneToMany @JoinColumn(name = "patient_id") private Collection<Vehicle> patientVehicles = new ArrayList<Vehicle>(); } @Entity class Vehicle { }
A bidirectional association via a foreign key
@Entity class Patient { @OneToMany(mappedBy = "patient") private Collection<Vehicle> patientVehicles = new ArrayList<Vehicle>(); } @Entity class Vehicle { @JoinColumn(name="patient_id") private Patient patient }
This is the basic starting point of using
@JoinColumn
.To verify that the foreign key(
patient_id
in theVehicle
table) is really mapped in the patients table you can use@JoinColumn(nullable = false)
@Entity class Vehicle { @JoinColumn(name="patient_id", nullable = false) private Patient patient }
这篇关于什么是@JoinColumn,以及它如何在Hibernate中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!