Asosiasi searah melalui tabel gabungan
@Entity
class Patient {
@OneToMany
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
}
Asosiasi dua arah melalui tabel gabungan
@Entity
class Patient {
@OneToMany
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
@ManyToOne(fetch = FetchType.LAZY)
private Patient patient;
}
Asosiasi searah melalui kunci asing
@Entity
class Patient {
@OneToMany
@JoinColumn
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
}
Asosiasi dua arah melalui kunci asing
@Entity
class Patient {
@OneToMany(mappedBy = "patient")
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
}
@Entity
class Vehicle {
@ManyToOne(fetch = FetchType.LAZY)
private Patient patient;
}
Asosiasi dua arah melalui kunci asing dengan spesifikasi nama kolom asing
@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;
}
Ini adalah titik awal dasar menggunakan @JoinColumn
.
Untuk memverifikasi bahwa kunci asing(patient_id
di Vehicle
table) benar-benar dipetakan di tabel pasien, Anda dapat menggunakan @JoinColumn(nullable = false)
@Entity
class Vehicle {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="patient_id", nullable = false)
private Patient patient
}