Hibernate One-To-One Mapping Example using Annotation (Shared Primary Key)

The Hibernate One-To-One Mapping Example using Annotation tutorial show you how to use Hibernate One-To-One Unidirectional Shared primary key mapping. With One-To-One Unidirectional Shared primary key mapping, two tables share the same primary key.

In this Hibernate One-To-One Mapping Example using Annotation tutorial, we are discussing an example of a one-to-one association with a Student entity that references one StudentDetail entity. The StudentDetail does not reference back to the Student so that the reference is said to be unidirectional, in one direction only.

Other interesting posts you may like

Create Database Table

We create two tables with these below scripts:

Project directory structure

Our project is created with the below project structure:

Hibernate One-To-One Mapping Example using Annotation

Maven dependencies

We provide the required Hibernate and MySQL dependency in pom.xml like this:

Create Model class

Model class Student and StudentDetail are simple POJO class. Here we are using class Student and StudentDetail with JPA annotations to map them to a database tables (these tables were created in above step).

Student Model class

StudentDetail Model class

Let’s dig deepr:

@OneToOne on studentDetail property of Student class indicates that there is a one-to-one association from Student to StudentDetail.
@PrimaryKeyJoinColumn indicates that the primary key of the Student entity is used as a foreign key to the StudentDetail entity.
Here we are using cascade = CascadeType.ALL. It means that StudentDetail entity can not exist without Student entity and StudentDetail entity will be update/delete on subsequent update/delete on Student entity.

Create Hibernate configuration file

We need to provide for hiberate all stuffs like database dialect, driver class, url and account information to connect database. These stuffs is declared in file hibernate.cfg.xml like this:

This file is placed in src/main/resources folder.

Create Hibernate Utility class

For configuring hibernate on startup and managing session factory we create the HibernateUtil class like this:

Create program to demonstrate Hibernate One-To-One Mapping
Here is a final part of the Hibernate One-To-One Mapping Example using Annotation tutorial. We will create the main class like below:

In the above program, we persist a Student early, so that id of that student is generated. Then we set the StudentDetail id with student id( it means that foreign key constraint be respected). Finally, the studentDetail property of Student is set by StudentDetail object and save Student.

The Cascade attribute is used on studentDetail property of Student class so that Hibernate will save StudentDetail object automatically and we no need to save it explicitly.

Run the above program as Java application. We will get the ouput like this:

That’s it on the Hibernate One-To-One Mapping Example using Annotation tutorial.

Download complete source code, please click link below

OneToOneMappingExample.zip (32 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons