举报投诉联系我们 手机版 热门标签 鳄鱼CMS
您的位置:鳄鱼CMS > jpa 级联删除 JPA 删除关系级联示例

jpa 级联删除 JPA 删除关系级联示例

2023-03-20 20:31 Java教程

jpa 级联删除 JPA 删除关系级联示例

jpa 级联删除

JPA 级联删除是指在实体之间建立级联关系,当删除一个实体时,会自动删除其关联的实体。JPA 的级联删除可以用来减少数据库中的冗余数据,从而提高数据库性能。

JPA 的级联删除可以通过 @OneToMany 或 @ManyToMany 注释来实现。@OneToMany 注释用于一对多的关系,@ManyToMany 注释用于多对多的关系。下面是一个使用 @OneToMany 来实现 JPA 级联删除的例子:

@Entity 
public class Parent { 

    @Id 
    private Long id; 

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) 
    private List<Child> children; 

    // getters and setters... 
} 
@Entity 
public class Child { 

    @Id 
    private Long id; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "parent_id") 
    private Parent parent;  

    // getters and setters...  
}

JPA 删除关系级联示例

JPA教程 - JPA删除关系级联示例


以下代码显示如何删除具有级联操作的相关实体。

例子

下面的代码来自PersonDaoImpl.java。

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

    Phone ph = new Phone();
    ph.setEmployee(emp);
    ph.setNumber("1234567890");

    ParkingSpace ps = new ParkingSpace();
    ps.setEmployee(emp);
    ps.setLocation("Dept");
    
    emp.setParkingSpace(ps);
    emp.getPhones().add(ph);
    em.persist(emp);
    emp = em.find(Employee.class, 1);
    em.remove(emp);
    
  }

  @PersistenceContext
  private EntityManager em;
}

以下代码来自Phone.java。

package cn..common;



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

@Entity
public class Phone {
    @Id
    private long id;
    private String number;
    private String type;
    @OneToOne Employee employee;
    
    public long getId() {
        return id;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    
    public String getNumber() {
        return number;
    }
    
    public void setNumber(String phoneNo) {
        this.number = phoneNo;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String phoneType) {
        this.type = phoneType;
    }
    
    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public String toString() {
        return "Phone id: " + getId() + 
               ", no: " + getNumber() +
               ", type: " + getType();
    }
}

以下代码来自Employee.java。

package cn..common;



import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Employee {
    @Id
    private int id;
    private String name;
    
    @OneToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
    @JoinColumn(name="PSPACE_ID") 
    ParkingSpace parkingSpace;

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE},
               mappedBy="employee")
    Collection<Phone> phones;

    public Employee() {
        phones = new ArrayList<Phone>();
    }
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public Collection<Phone> getPhones() {
      return phones;
    }

    public void setPhones(Collection<Phone> phones) {
      this.phones = phones;
    }

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

    public ParkingSpace getParkingSpace() {
        return parkingSpace;
    }
    
    public void setParkingSpace(ParkingSpace parkingSpace) {
        this.parkingSpace = parkingSpace;
    }

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

下面的代码来自ParkingSpace.java。

package cn..common;



import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="PARKING_SPACE")
public class ParkingSpace {
    @Id
    private int id;
    private int lot;
    private String location;
    @OneToOne(mappedBy="parkingSpace")
    private Employee employee;

    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }

    public int getLot() {
        return lot;
    }

    public void setLot(int lot) {
        this.lot = lot;
    }
    
    public String getLocation() {
        return location;
    }
    
    public void setLocation(String deptName) {
        this.location = deptName;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    
    public String toString() {
        return "ParkingSpace id: " + getId() + " lot: " + getLot() +
               ", location: " + getLocation();
    }
}
下载 Remove_With_Relationships_Cascade.zip

以下是数据库转储。

Table Name: EMPLOYEE




Table Name: PARKING_SPACE
Table Name: PHONE


阅读全文
以上是鳄鱼CMS为你收集整理的jpa 级联删除 JPA 删除关系级联示例全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 鳄鱼CMS eyucms.com 版权所有 联系我们