什么是@JoinColumn以及如何在Hibernate中使用它

zbr*_*yan 24 java postgresql hibernate jpa

我一直在阅读很多关于@JoinColumn的内容,但我仍然不了解它背后的想法.

病人表

CREATE TABLE patient (
patient_id BIGINT NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
PRIMARY KEY(patient_id));
Run Code Online (Sandbox Code Playgroud)

车辆表

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));
Run Code Online (Sandbox Code Playgroud)

患者类

@OneToMany(mappedBy = "patient")
    private Collection<Vehicle> patientVehicles = new ArrayList<Vehicle>();
Run Code Online (Sandbox Code Playgroud)

车辆类

@ManyToOne
@JoinColumn(name="patient_id")
private Patient patient;
Run Code Online (Sandbox Code Playgroud)

我对车辆类如何部分感到困惑,之间的关系是什么

Vehicle Class ---- Entity
@JoinColumn(name="patient_id") ---- annotation
private Patient patient ----field
Run Code Online (Sandbox Code Playgroud)

是吗?该车辆实体有一个外键患者实体命名patient_id.将patient_id添加为Vehicle Entity表中的列

JoinColumn的name参数应该始终是外键还是主键?

我一直在读这个,但我仍然感到困惑. JPA JoinColumn vs mappedBy

v.l*_*nev 36

通过连接表进行单向关联

@Entity
class Patient {

    @OneToMany
    private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();

}

@Entity
class Vehicle {

}
Run Code Online (Sandbox Code Playgroud)

通过连接表进行双向关联

@Entity
class Patient {

    @OneToMany
    private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();

}

@Entity
class Vehicle {

    @ManyToOne(fetch = FetchType.LAZY)
    private Patient patient;

}
Run Code Online (Sandbox Code Playgroud)

通过外键进行单向关联

@Entity
class Patient {

    @OneToMany
    @JoinColumn
    private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();

}

@Entity
class Vehicle {

}
Run Code Online (Sandbox Code Playgroud)

通过外键进行双向关联

@Entity
class Patient {

    @OneToMany(mappedBy = "patient")
    private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();

}

@Entity
class Vehicle {

    @ManyToOne(fetch = FetchType.LAZY)
    private Patient patient;

}
Run Code Online (Sandbox Code Playgroud)

这是使用的基本出发点@JoinColumn.

要验证外键(patient_idVehicle表中)是否真正映射到您可以使用的患者表中@JoinColumn(nullable = false)

@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;

}
Run Code Online (Sandbox Code Playgroud)


Vis*_*gam 5

Vehicle Class ---- Entity @JoinColumn(name="patient_id")---- 注解 private Patient 病人 ---- 字段

上面的代码将在车辆类中生成一个列patient_id(外键),该列将指向患者类主键。

MappedBy- 该属性告诉我们该关系将由 Vehicle 类管理。例子。如果我们插入一辆车,如果cascadetype为all/save,那么将会注入两条SQL。第一个 SQL 将在 Patient 表中注入详细信息,第二个 SQL 将在车辆表中注入车辆详细信息,其中车辆列的 Patient_id 列指向插入的患者元组。

  • @zenlloyd。要正确映射患者对象,您需要确保在车辆对象中设置了患者属性。如果未设置该值,则患者_id 将被注入空值。 (2认同)

Lov*_*uri 5

连接列是用@JoinColumn 注释声明的,它看起来像@Column 注释。它还有一个名为referencedColumnName 的参数。此参数声明目标实体中将用于连接的列。

在双向关系中,其中一侧(并且只有一侧)必须是所有者:所有者负责关联列的更新。为了声明不负责关系的一方,使用了属性mappedBy。mappingBy 指的是所有者侧关联的属性名称。

这是示例代码:

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;
Run Code Online (Sandbox Code Playgroud)