JPA Tutorial - JPA OneToOne Map Example








This section shows how to do one to one map in JPA mapping.

This example assumes that one Person can only be part of one department and one department can only have one person.

In Person entity we mark the Department reference property with @OneToOne annotation.

@Entity
public class Person {

...
@OneToOne
private Department department;
...

Here is the simple code to setup the two entities and save them to database.

    Person p1 = new Person("Tom");
    
    p1.setName("Tom");
    
    Department d = new Department();
    d.setName("Design");
    p1.setDepartment(d);
    
    em.persist(p1);
    em.persist(d);




Example

The following code is from Person.java.

package com.java2s.common;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Person {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
   
  private String name;
  @OneToOne
  private Department department;
  
  public Person() {}

  public Person(String name) {
    this.name = name;
  }


  public Department getDepartment() {
    return department;
  }

  public void setDepartment(Department department) {
    this.department = department;
  }

  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "Person [id=" + id + ", name=" + name + "]";
  }

}

The following code is from Department.java.

package com.java2s.common;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Department {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
  
  private String name;

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

The following code is from PersonDaoImpl.java.

package com.java2s.common;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test(){
    Person p1 = new Person("Tom");
    
    p1.setName("Tom");
    
    Department d = new Department();
    d.setName("Design");
    p1.setDepartment(d);
    
    em.persist(p1);
    em.persist(d);
  }
  @PersistenceContext
  private EntityManager em;
}


Download OneToOne_Map.zip

The following is the database dump.

Table Name: DEPARTMENT
 Row:
    Column Name: ID,
    Column Type: BIGINT:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: Design
Table Name: PERSON
 Row:
    Column Name: ID,
    Column Type: BIGINT:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: Tom

    Column Name: DEPARTMENT_ID,
    Column Type: BIGINT:
    Column Value: 1




OneToOne Lazy Load

The following code shows how to do one to one mapping with lazy load setting.