How to create one to one relationship in Hibernate 4 with Annotation?

Annotations is now widely used in the development of Java programming and Hibernate also supports this. Programmers can take the advantages of this for developing applications quickly. This tutorial teaches you how to develop relationship in Hibernate using annotations.

How to create one to one relationship in Hibernate 4
with Annotation?

In this tutorial we are developing an application in Hibernate 4 to create
one-to-one mapping. In this example we have two entities worker
and workerdetails. These two entities are mapped to each other
through one-to-one mapping and we can use the feature of Hibernate while saving
the entities.

This tutorial is using Java 5 annotations for defining the mapping between
these two domain entities. We have used MySQL database as backend server but you
can also update Hibernate configuration to use other database database of your
choice. MySQL is easily to install and small server but its powerful enough to
manage huge databases.

Why use annotations in Hibernate?

Annotations was first introduced in Java 5 and now this features is one of
most widely used features in Java programming. Hibernate 4 framework increases
the productivity of the developers working on development of persistence layer.
Annotations further added simplicity in programming which makes coding much
easier and hence it increases the productivity of the developer.

Developers working on Hibernate framework finds annotations much useful while
making changes or doing the maintenance of the software. For example when there
is any change in database structure due to change in business logic or new
requirement, developers can quickly change the annotated entity class. This
makes the process of application maintenance much easier as there is no
requirement of changing the model class and mapping information separately.

What is one to one mapping?

One to one mapping is the mapping in which exactly one record from Table A
maps to one record in Table B and vice versa.

In this case record of one table have only one related record
in the other table. In this tutorial we have use two entities worker and
workerdetail. One record in worker table maps to only one record in the
workerdetail table.

Simply, one record in the worker table maps to only one address record in the workerdetail
table.

Example of One to One mapping

In this example we will add the data into two tables into the database and
data in both the tables will be inserted using one save statement. Both the table worker and workerdetail is
related with
each other through foreign key worker_id field. In the example given below,
Hibernate 4 annotation based one to one mapping is accomplished.

We have used the Annotated model class in the the example code which
simplifies the development work as explained above.

Here is the structure of the project which shows the required jar files:

The sql query used to create the tables worker and workerdetail
is given below :