什么是@JoinColumn 以及它在 Hibernate 中的使用方式 [英] what is @JoinColumn and how it is used in Hibernate

查看:28
本文介绍了什么是@JoinColumn 以及它在 Hibernate 中的使用方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于@JoinColumn 的文章,但我仍然不明白它背后的想法.

I have been reading a lot about @JoinColumn but I still don't get the idea behind it.

病床

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

患者分类

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

车辆类别

@ManyToOne
@JoinColumn(name="patient_id")
private Patient patient;

我很困惑Vehicle类是如何部分的,它们之间的关系是什么

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

有没有说;车辆实体有一个外键患者实体,名为patient_id.将 patient_id 添加为车辆实体表

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

JoinColumn 的名称参数是否应该始终是外键或主键?

Do the name parameter of the JoinColumn should always be a Foreign Key or Primary Key?

我一直在读这个,但我仍然很困惑.JPA JoinColumn 与 mappingBy

I have been reading this but I'm still confuse. JPA JoinColumn vs mappedBy

推荐答案

通过连接表的单向关联

@Entity
class Patient {

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

}

@Entity
class Vehicle {

}

通过连接表的双向关联

@Entity
class Patient {

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

}

@Entity
class Vehicle {

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

}

通过外键的单向关联

@Entity
class Patient {

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

}

@Entity
class Vehicle {

}

通过外键的双向关联

@Entity
class Patient {

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

}

@Entity
class Vehicle {

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

}

通过具有外列名称规范的外键的双向关联

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

}

这是使用@JoinColumn的基本起点.

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

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)

@Entity
class Vehicle {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="patient_id", nullable = false)
    private Patient patient

}

这篇关于什么是@JoinColumn 以及它在 Hibernate 中的使用方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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