Joined Table

All lectures for UZ purposes
Daraja, Dars
Mavjud

Tavsif

Ma'lumotlar bazasida sinflar ierarxiyasini saqlashning yana bir strategiyasi Joined Table deb ataladi. Buning uchun maxsus annotatsiya mavjud:


@Inheritance(strategy = InheritanceType.JOINED)

Bizning sinflarimizning misoli:


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

Ushbu annotatsiyadan foydalanganda Hibernate har bir sinf va uning ost sinflari uchun alohida jadval kutadi. Ulardan ma'lumot olish uchun SQL-operatori JOIN'ni ishlatishga to'g'ri keladi.

Ma'lumotlar bazasi sxemasining misoli:


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
}

Agar jadvaldan biror klientning ma'lumotlarini olishga qaror qilsang, Hibernate jadvalarni birlashtirish uchun JOIN'ni ishlatishga to'g'ri keladi:

SELECT u.id, u.name, u.birthday, c.address FROM user u JOIN client c ON u.id = c.id;

@PrimaryKeyJoinColumn

Qarama-qarshi Entity-sinflar jadvalda ota Entity-sinfining id ob'ektiga murojaat qiluvchi ustun bor. Ushbu ustunning nomi odatda ota sinf ustunining nomi bilan bir xil.

Misol:


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

Bazada jadvallar shunday ko'rinishda bo'ladi:


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
}

Agar ustunning nomini qarama-qarshi jadvallarda qayta belgilashni xohlasang, annotatsiyani ishlatishingiz kerak @PrimaryKeyJoinColumn. Misol:


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

Bazada jadvallar shunday ko'rinishda bo'ladi:


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
}
Izohlar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION