One-to-One Mapping

One-to-One Mapping

A one-to-one relationship is identical to a many-to-one association, except that the column is specified to be unique. An address object, for example, can be linked to a single employee object.

In a relational database, a one-to-one connection occurs when a parent table record has zero or one child record in the child table. In this tutorial, we'll use XML configuration to build one-to-one mapping in Hibernate (relationship).

In Hibernate, one to one mapping may be accomplished in two ways.

  • With a foreign key, one to one mapping is possible.
  • With a primary key, there is a one-to-one mapping.

In basic words, a One-to-One connection is identical to a Many-to-One relationship with the exception that the column will be set to unique, i.e. two entities are said to be in a One-to-One relationship if one entity has just one occurrence in the other entity. An address object, for example, can be linked to a single employee object. On the other hand, these relationships are infrequently utilised in relational table models, and therefore we won't require this mapping very often.

Department dept

To define a One to One relationship using hibernate, annotation @OneToOne needs to be placed before the class reference variable declaration or before the getter method of this reference variable.


Department dept;


@OneToOne public void getDept(){ return dept; }

Now, we must call the setter method for the department reference variable into the OneToOneMapping class bypassing the department class object.