Katrangan
Strategi liyane kanggo nyimpen hirarki kelas ing basis data diarani Tabel Gabungan. Ana anotasi khusus kanggo iki:
@Inheritance(strategy = InheritanceType.JOINED)
Conto kelas kita:
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
class User {
int id;
String name;
LocalDate birthday;
}
@Entity
class Employee extends User {
String occupation;
int salary;
LocalDate join;
}
@Entity
class Client extends User {
String address;
}
Nalika nggunakake anotasi iki, Hibernate bakal nyana tabel kapisah ing database kanggo saben kelas lan subclasses. Nalika milih data saka wong-wong mau, sampeyan kudu nggunakake operator SQL JOIN.
Contoh skema database:
CREATE TABLE user {
id INT,
name VARCHAR,
birthday DATE
}
CREATE TABLE employee {
id INT,
occupation VARCHAR,
salary INT,
join DATE
}
CREATE TABLE client {
id INT,
address VARCHAR
}
Yen sampeyan arep njupuk data saka sawetara klien saka meja, banjur Hibernate kudu nggunakake JOIN kanggo nggabungake tabel:
SELECT u.id, u.name, u.birthday, c.address FROM user u JOIN client c ON u.id = c.id;
@PrimaryKeyJoinColumn
Kelas Entitas Anak duwe kolom ing tabel sing nuduhake id obyek saka kelas Entitas induk. Jeneng kolom iki minangka standar padha karo jeneng kolom kelas induk.
Tuladha:
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
class User {
@Id
int user_identifier;
String name;
LocalDate birthday;
}
@Entity
class Employee extends User {
String occupation;
int salary;
LocalDate join;
}
@Entity
class Client extends User {
String address;
}
Banjur tabel database bakal katon kaya iki:
CREATE TABLE user {
user_identifier INT,
name VARCHAR,
birthday DATE
}
CREATE TABLE employee {
user_identifier INT,
occupation VARCHAR,
salary INT,
join DATE
}
CREATE TABLE client {
user_identifier INT,
address VARCHAR
}
Yen sampeyan pengin ngganti jeneng kolom ing tabel gumantung, sampeyan kudu nggunakake anotasi @PrimaryKeyJoinColumn . Tuladha:
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
class User {
@Id
int user_identifier;
String name;
LocalDate birthday;
}
@Entity
@PrimaryKeyJoinColumn(name=”user_id”)
class Employee extends User {
String occupation;
int salary;
LocalDate join;
}
@Entity
@PrimaryKeyJoinColumn(name=”user_id2”)
class Client extends User {
String address;
}
Banjur tabel database bakal katon kaya iki:
CREATE TABLE user {
user_identifier INT,
name VARCHAR,
birthday DATE
}
CREATE TABLE employee {
user_id INT,
occupation VARCHAR,
salary INT,
join DATE
}
CREATE TABLE client {
user_id2 INT,
address VARCHAR
}
GO TO FULL VERSION