top of page

Hibernate One To One Relationship Using Annotations (Bidirectional).


In this post we are going to explain one to one bidirectional relationship in hibernate using annotations and also the use of mappedBy attribute and @PrimaryKeyJoinColumn.


Technologies used in this article :

  1. Maven 4.0.0

  2. JDK 1.8

  3. Hibernate 4.3.5.Final

  4. MySql 5.1.34


In this example you will learn how to map one-to-one bidirectional relationship using Hibernate. Consider the following relationship between Student and StudentDeatails entity.

The term "Bidirectional" means "functioning in Both direction", which means we are able to access Object B from Object A and Object A from Object B. Also it allows you to apply cascading options in both the directions.

Here arrows shows us bidirectional relationship between two entities i.e. we are able to access StudentDetails from Student Object and vice versa.


1. Project Structure , Dependencies and Configuration.


For project structure, required dependencies and for configuration please refer to our previous post Hibernate One To One Relationship Using Annotations (Unidirectional).


2. Hibernate Mapping.


File : Student.java



















































File : StudentDetails.java






























































For bidirectional mapping we have to define Student class object in StudentDetails class and also annotate it with @OneToOne annotation.


2. Testing.


File : SaveTester.java

































Output :




students Table:

student_details Table:








File : QueryTester.java

Output :


As we can see we did query for StudentDetails object and with StudentDetails object we get Student object also in output.



3. Hibernate Mapping Using mappedBy attribute.


In Student class use mappedBy attribute of @OneToOne annotation. This attribute corresponding value maps to the corresponding field name of Student object in StudentDetails class.


MappedBy signals hibernate that the key for the relationship is on the other side i.e on StudentDetails side.










Output :

When we run SaveTester.java class than we find below result.



Note*: But Now this time we get mapped column or join column in other side i.e. in student_details table.

So we get an extra column for mapping in student_details table.


students Table:







student_details Table:







4. @PrimaryKeyJoinColumn Annotation.


File : Student.java










File : StudentDetails.java









@PrimaryKeyJoinColumn Annotation:

The @PrimaryKeyJoinColumn annotation is used to map primary key of entity as foreign key to reference the mapped entity.


Output :

When we run SaveTester.java class than we find below result.



















students Table:










student_details Table:









Note*: We can see in tables there is no extra column to map entities because here we used @PrimaryKeyJoinColumn annotation to map entities.By using this we mapped student_details primary key with students table primary key.


Find related source code on GITHUB.


In next post we are going to explain Hibernate One To Many relationship.

For updates of our next post please subscribe below and don't forget to like. In case of any queries write in comment section.

Featured Posts
Recent Posts
Archive
Related Posts
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page