JPA one to one mapping

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, continuing jpa association mappings as in previous article In this article we will learn about jpa one to one mapping.

Introduction

JPA one to one mapping is specified in cases where an entity is mapped to only one other entity. Consider a scenario such that every a Desk is allocated to a specific Employee.

  • The Employee table has primary key idemployee.
  • The Desk table has primary key iddesk;
  • Employee table has a column iddesk which is foreign key for iddesk primary key column in Desk table.

Unidirectional JPA one to one mapping:

EmployeeToDesk

Employee entity is as follows:

Desk entity is as follows:

Annotations used here are:

Bidirectional JPA one to one mapping:

When Desk field is annotated with  @OneToOne  annotation in Employee entity, you can access the desk with employee object. But if you want to be able to access Employee object with the desk he/she has been assigned, then you will need to create employee field in Desk entity and map it to Employee .

Add following to Desk entity.

There are two rules about bidirectional jpa one to one mapping.

  • The @JoinColumn annotation goes on mapping of the entity that is mapped to the table containing the join colulmn.The owner of relationship. In our case, Employee table has the join column so @JoinColumn is on Desk field of Employee entity.
  • The mappedBy element should be specified in the @OneToOne association in the entity that reverse side of relationship. i.e. The entity which does not provide join column on database aspect. In our case, Desk is the inverse entity.

Test the code using

You will see the a record inserted into database and employee object printed on the console.

I hope this helped understand the concept of jpa one to one mapping.

References for further study :

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

4 thoughts on “JPA one to one mapping

  • Pingback:JPA one to many mapping | theJavaGeek

  • October 4, 2016 at 1:05 pm
    Permalink

    Hello Prasad,

    What could be the problem, when the employee object is printed on the console but no record is inserted in the database ?

    Thank You,

    Reply
  • January 9, 2018 at 12:41 pm
    Permalink

    desk = em.find(Desk.class, 1); // retrieves desk from database
    desk.setEmployee(employee);
    System.out.println(desk.getEmployee());

    Not sure why desk.setEmployee is called in the above code, doesn’t it get populated by em in the find call?

    Reply
    • January 14, 2018 at 12:14 pm
      Permalink

      As per jpa specifications, all entity relationships need to he specified programmatically

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *