JPA 删除关系示例

JPA教程 – JPA删除关系示例

我们可以通过将引用设置为null来删除映射中的关系,如下所示。

以下代码删除了地址和员工之间的链接,然后从数据库中删除该地址。

    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);

例子

下面的代码来自PersonDaoImpl.java。

package cn.w3cschool.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;
}

以下代码来自Address.java。

package cn.w3cschool.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();
    }

}

以下代码来自Employee.java。

package cn.w3cschool.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();
    }
}

下载 Remove_Relationship.zip

以下是数据库转储。

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
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《JPA 删除关系示例》
文章链接:https://zhuji.vsping.com/293535.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。