In this method, we will create a relationship in such a way that both tables contain the same primary key value for the related record. So, we can say that here we used unidirectional as well as bidirectional relationships, because we can get either record through another record using its primary key. For example, if Person
is inserted with id 1
, you should get the PassportDetail
record inserted with id 1
as well.
Here, we will create Person
and PassportDetail
classes to work this demo.
Use the following script to create the tables if you are not using hbm2dll=create|update
:
Use the following script to create the passport_detail
table:
CREATE TABLE `passport_detail` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `passportno` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) );
Use the following script to create the person
table:
CREATE TABLE `person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK_PASSPORT_DETAIL_ID` (`id`), CONSTRAINT `FK_PASSPORT_DETAIL_ID` FOREIGN KEY (`id`) REFERENCES `passport_detail` (`id`) );
Here, we will use Person
and PassportDetail
classes:
Source file: Person.java
@Entity @Table(name = "person") public class Person { @Id @GeneratedValue @Column(name = "id") private long id; @Column(name = "name") private String name; @OneToOne(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn private PassportDetail passportDetail; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public PassportDetail getPassportDetail() { return passportDetail; } public void setPassportDetail(PassportDetail passportDetail) { this.passportDetail = passportDetail; } }
Source file: PassportDetail.java
@Entity @Table(name = "passport_detail") public class PassportDetail { @Id @GeneratedValue @Column(name = "id") private long id; @Column(name = "passportno") private String passportNo; @OneToOne( mappedBy = "passportDetail" , cascade = CascadeType.ALL ) private Person person; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getPassportNo() { return passportNo; } public void setPassportNo(String passportNo) { this.passportNo = passportNo; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
In this section, we will take a look at how to insert a record step by step.
Use the following code to insert a record in to the database. Here, we will insert a Person
with a PassportDetail
object:
PassportDetail detail = new PassportDetail(); detail.setPassportNo("G44244781"); Person person = new Person(); person.setId(1); person.setName("Virendra"); person.setPassportDetail(detail); Transaction transaction = session.getTransaction(); transaction.begin(); session.save(person); transaction.commit();
Let's start with the changes in the PassportDetail
class.
The first change is in @OneToOne(cascade = CascadeType.ALL)
.
Here, we used @OneToOne
with the CascadeType.ALL
option, which informs hibernate to create a one-to-one relationship and apply cascading for all operations.
The changes in the Person
class are as follows:
@PrimaryKeyJoinColumn
private PassportDetail passportDetail;
The @PrimaryKeyJoinColumn
annotation is used with the PassportDetail
class, which creates a reference between the primary key of the Person
class and the primary key of the PassportDetail
class.