@OneToOne

All lectures for JV purposes
tingkat , wulangan
kasedhiya

5.1 Macem-macem wujud komunikasi siji-siji

Ana kasus liyane sing menarik lan luwih spesifik babagan hubungan antarane rong kelas Entitas - hubungan siji-kanggo-siji.

Aku nelpon kasus iki khusus banget, amarga luwih akeh babagan obyek Jawa tinimbang database. Ing basis data, mung ana rong pilihan kanggo hubungan antarane tabel:

  • Baris tabel ngemot pranala menyang id saka tabel liyane.
  • Tabel layanan digunakake kanggo akeh-kanggo-akeh sesambetan.

Ing kasus kelas Entitas, bisa uga ana opsi sing diterangake dening sawetara anotasi:

  • @Ditempelake
  • OneToOne siji-sisi
  • Bilateral OneToOne
  • @MapsId

Ing ngisor iki kita bakal nimbang sing paling populer.

5.2 Ditempelake

Miturut cara, kita wis nganggep pilihan komunikasi siji-kanggo-siji sing paling gampang - iki minangka anotasi @Embedded. Ing kasus iki, kita duwe rong kelas sing disimpen ing tabel sing padha ing database.

Contone, kita pengin nyimpen alamat pangguna ing kelas UserAddress :


@Embeddable
class UserAddress {
   @Column(name="user_address_country")
   public String country;
   @Column(name="user_address_city")
   public String city;
   @Column(name="user_address_street")
   public String street;
   @Column(name="user_address_home")
   public String home;
}

Banjur kita mung kudu nambah lapangan kanthi alamat iki menyang kelas Panganggo :


@Entity
@Table(name="user")
class User {
   @Column(name="id")
   public Integer id;
 
   @Embedded
   public UserAddress address;
 
   @Column(name="created_date")
   public Date createdDate;
}

Hibernate bakal nindakake liyane: data bakal disimpen ing siji meja, nanging nalika nulis pitakon HQL, sampeyan kudu operate ing lapangan kelas.

Tuladha pitakon HQL:

select from User where address.city = 'Paris'

5.3 OneToOne siji-sisi

Mbayangno saiki kahanan: kita duwe pegawe Tabel sumber lan tugas sing nuduhake pegawe. Nanging kita ngerti manawa maksimal siji tugas bisa ditugasake kanggo siji pangguna. Banjur kita bisa nggunakake anotasi kanggo njlèntrèhaké kahanan iki @OneToOne.

Tuladha:


@Entity
@Table(name="task")
class EmployeeTask {
   @Column(name="id")
   public Integer id;
 
   @Column(name="name")
   public String description;
 
   @OneToOne
   @JoinColumn(name = "employee_id")
   public Employee employee;
 
   @Column(name="deadline")
   public Date deadline;
}

Hibernate bakal mesthekake yen ora mung siji tugas duwe siji pangguna, nanging uga siji pangguna mung duwe siji tugas. Yen ora, kasus iki meh ora beda karo @ManyToOne.

5.4 Bilateral OneToOne

Opsi sadurunge bisa uga ora nyenengake, amarga asring sampeyan pengin menehi karyawan ora mung kanggo tugas, nanging uga menehi tugas kanggo karyawan.

Kanggo nindakake iki, sampeyan bisa nambah lapangan EmployeeTask menyang kelas Employee lan menehi anotasi sing bener.


@Entity
@Table(name="employee")
class Employee {
   @Column(name="id")
   public Integer id;
 
   @OneToOne(cascade = CascadeType.ALL, mappedBy="employee")
   private EmployeeTask task;
}

Penting!Tabel karyawan ora duwe kolom task_id , nanging kolom employee_id ing tabel tugas digunakake kanggo nggawe hubungan antarane tabel .

Nggawe sambungan antarane obyek katon kaya iki:


Employee director = session.find(Employee.class, 4);
EmployeeTask task = session.find(EmployeeTask.class, 101);
task.employee = director;
director.task = task;
 
session.update(task);
session.flush();

Kanggo mbusak pranala, pranala uga kudu dibusak saka loro obyek:


Employee director = session.find(Employee.class, 4);
EmployeeTask task = director.task;
 
task.employee = null;
session.update(task);
 
director.task = null;
session.update(director);
 
session.flush();

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION