JPA Tutorial - JPA Remove Relationship Example








We can remove a relationship in the mapping by setting the reference to null as follows.

The following code removes the linking between an address and a employee and then remove the address from the database.

    Employee emp = new Employee();
    emp.setName("Tom");
    emp.setId(1);

    Address addr = new Address();
    addr.setId(1);
    addr.setStreet("street");
    addr.setCity("city");
    addr.setState("state");
    emp.setAddress(addr);
    em.persist(emp);

    emp = em.find(Employee.class, 1);
    addr = emp.getAddress();
    emp.setAddress(null);
    em.remove(addr);




Example

The following code is from PersonDaoImpl.java.

package com.java2s.common;

import java.util.Date;

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

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test() {
    Employee emp = new Employee();
    emp.setName("Tom");
    emp.setId(1);

    Address addr = new Address();
    addr.setId(1);
    addr.setStreet("street");
    addr.setCity("city");
    addr.setState("state");
    emp.setAddress(addr);
    em.persist(emp);

    emp = em.find(Employee.class, 1);
    addr = emp.getAddress();
    emp.setAddress(null);
    em.remove(addr);
  }

  @PersistenceContext
  private EntityManager em;
}

The following code is from Address.java.

package com.java2s.common;


import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Address {
    @Id
    private int id;
    private String street;
    private String city;
    private String state;
    private String zip;
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getStreet() {
        return street;
    }
    
    public void setStreet(String address) {
        this.street = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }
    public String toString() {
        return "Address id: " + getId() + 
               ", street: " + getStreet() +
               ", city: " + getCity() +
               ", state: " + getState() +
               ", zip: " + getZip();
    }

}

The following code is from Employee.java.

package com.java2s.common;


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Employee {
    @Id
    private int id;
    private String name;
    
    @ManyToOne(cascade=CascadeType.PERSIST)
    Address address;

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

    public Address getAddress() {
        return address;
    }
    
    public void setAddress(Address address) {
        this.address = address; 
    }

    public String toString() {
        return "Employee id: " + getId() + " name: " + getName() +
               " with " + getAddress();
    }
}


Download Remove_Relationship.zip

The following is the database dump.

Table Name: ADDRESS




Table Name: EMPLOYEE
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

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

    Column Name: ADDRESS_ID,
    Column Type: INTEGER:
    Column Value: null