JPA Tutorial - JPA OneToOne Map Cascade All Example








The following code shows how to set cascade action for one to one mapping.

  @OneToOne(cascade=CascadeType.ALL,mappedBy="department")
  private Person person;

Example

The following code is from Department.java.

package com.java2s.common;

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

@Entity
public class Department {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
  
  private String name;
  
  @OneToOne(cascade=CascadeType.ALL,mappedBy="department")
  private Person person;
  
  public Person getPerson() {
    return person;
  }

  public void setPerson(Person person) {
    this.person = person;
  }

  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 "Department [id=" + id + ", name=" + name + ", person=" + person
        + "]";
  }
}

The following code is from PersonDaoImpl.java.

package com.java2s.common;


import java.util.List;

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

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);
    d.setPerson(p1);
    
    em.persist(p1);
    em.persist(d);
    
    Query query = em.createQuery("SELECT e FROM Person e");
    List<Person> list = (List<Person>) query.getResultList();
    System.out.println(list.get(0).getDepartment());
    
    em.remove(list.get(0));
    
  }
  @PersistenceContext
  private EntityManager em;
}

The following code is from Person.java.

package com.java2s.common;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Person {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
   
  private String name;
  
  @OneToOne(cascade=CascadeType.ALL)
  @JoinColumn(name="DEPT_ID")   
  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 + "]";
  }

}


Download OneToOne_Map_Cascade_All.zip