One-to-one mapping using a common primary key

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.

Getting ready

Here, we will create Person and PassportDetail classes to work this demo.

Creating the tables

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` ...

Get Java Hibernate Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.