Hibernate One-To-One Mapping Example using Foreign key Annotation

The Hibernate One-To-One Mapping Example using Foreign key Annotation tutorial shows you how to use Hibernate One-To-One Unidirectional Foreign Key association mapping using annotation based configuration. Here, one table has a foreign key column that references the primary key of associated table.

We are taking an example of Student and Address relationship. This relationship said that a student lives on one Address and one address can be occupied by only one student.

Other interesting posts you may like

Create Database Table

We create two tables with these below scripts:

Noticed that STUDENT table contains a foreign key (here is address_id field) referring to a primary key (here is id field) of ADDRESS table.

Create project directory structure

In this Hibernate One-To-One Mapping Example using Foreign key Annotation tutorial, we will create java project with final directory structure.

Hibernate One-To-One Mapping Example using Foreign key Annotation

Maven dependencies

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

Create Model classes

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

Student Model class

Address Model class

Let’s dig deeper:

@OneToOne on address field of Student class indicates that there is a one-to-one association from Student to Address.

We are using @JoinColumn annotation which maps on a seperate column ADDRESS_ID in STUDENT table. Here, You don’t still see the constraint between Student entity and Address entity, do you? Let’s look back the table creation step, you will see the foreign key between Student and ADDRESS table. Thanks to this constraint, the @JoinColumn also point to primary key of ADDRESS table.

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 Example using Foreign key Annotation

As discussed above, STUDENT table contains the foreign key referring to a primary key of ADDRESS table. So that we have to persisted address entity firstly then we set student’s address property and persist student.

Run main class and we get the output like below:

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

Download complete source code, please hit link below

OneToOneMappingForeignKeyAssociation.zip (29 downloads)

Source code on Github https://github.com/javabycode/hibernate-one-to-one-mapping-example-using-foreign-key-annotation

Leave a Comment

*

Please share it if you found this useful
Hide Buttons