JPA OneToMany级联操作详解之级联删除

原文地址:https://blog.csdn.net/eddle/article/details/6919552
下面给出了一个级联删除的demo,其中Garage与Auto是一个一对多的关系。
在级联删除的关键地方,就是在父栏添加CascadeType.REMOVE标注。

Garage.java
/**  
 * many to one 一对多关联 
 */ 
Java代码  
package com.jpa.bean1;   
import java.util.HashSet;   
import java.util.Set;   
import javax.persistence.CascadeType;   
import javax.persistence.Column;   
import javax.persistence.Entity;   
import javax.persistence.FetchType;   
import javax.persistence.GeneratedValue;   
import javax.persistence.Id;   
import javax.persistence.OneToMany;   
@Entity  
public class Garage {   
    private Integer gid;   
    private String garagenum;   
    private Set<Auto> autos = new HashSet<Auto>();         
    @Id @GeneratedValue  
    public Integer getGid() {   
        return gid;   
    }   
    public void setGid(Integer gid) {   
        this.gid = gid;   
    }   
    @Column(length=20)   
    public String getGaragenum() {   
        return garagenum;   
    }   
    public void setGaragenum(String garagenum) {   
        this.garagenum = garagenum;   
    }   
    //CascadeType属性有四个值,其中REMOVE属性是实现级联删除,要实现级联删除
    //在父栏必需添加CascadeType.REMOVE标注,这是级联删除的关键
    @OneToMany(cascade={CascadeType.REMOVE},mappedBy="garage")  
    public Set<Auto> getAutos() {   
        return autos;   
    }   
    public void setAutos(Set<Auto> autos) {   
        this.autos = autos;   
    }   
    public void addGarageAuto(Auto auto) {   
        auto.setGarage(this);   
        this.autos.add(auto);   
    }   
}  
Auto.java
/**  
 * one to many 多对一关联  
 */ 
Java代码  
package com.jpa.bean1;   
import javax.persistence.CascadeType;   
import javax.persistence.Entity;   
import javax.persistence.GeneratedValue;   
import javax.persistence.Id;   
import javax.persistence.JoinColumn;   
import javax.persistence.ManyToOne;   
@Entity  
public class Auto {    
    private Integer autoId;   
    private String autotype;   
    private String autonum;   
    private Garage garage;   
    @Id @GeneratedValue  
    public Integer getAutoId() {   
        return autoId;   
    }   
    public void setAutoId(Integer autoId) {   
        this.autoId = autoId;   
    }   
    public String getAutotype() {   
        return autotype;   
    }   
    public void setAutotype(String autotype) {   
        this.autotype = autotype;   
    }   
    public String getAutonum() {   
        return autonum;   
    }   
    public void setAutonum(String autonum) {   
        this.autonum = autonum;   
    }   
    @ManyToOne()   
    @JoinColumn(name="garageid")   
    public Garage getGarage() {   
        return garage;   
    }   
    public void setGarage(Garage garage) {   
        this.garage = garage;   
    }   
}  
级联删除操作前的持久化数据
mysql> select * from garage;
+-----+-----------+
| gid | garagenum |
+-----+-----------+
| 1 | room1 |
| 2 | room2 |
| 3 | room3 |
+-----+-----------+

mysql> select * from auto;
+--------+---------+----------+----------+
| autoId | autonum | autotype | garageid |
+--------+---------+----------+----------+
| 1 | hk2222 | car | 1 |
| 2 | bj0000 | car | 1 |
| 3 | jn1d31 | bus | 3 |
| 4 | sh3243 | car | 3 |
+--------+---------+----------+----------+  
junit测试方法delete()

Java代码  
@Test public void delete() {   
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");  
    EntityManager em = factory.createEntityManager();   
    em.getTransaction().begin();   
    Garage garage = em.find(Garage.class, 3);   
    em.remove(garage);   
    em.getTransaction().commit();   
    em.close();   
    factory.close();   

 @Test public void delete() {
  EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");
  EntityManager em = factory.createEntityManager();
  em.getTransaction().begin();
  Garage garage = em.find(Garage.class, 3);
  em.remove(garage);
  em.getTransaction().commit();
  em.close();
  factory.close();
 }
调用delete方法是实现级联删除,此时表garage中的gid为3的字段被全部删除,同时也级联删除了与garage相关联表auto中garageid为3的字段
级联删除后的持久化数据 
mysql> select * from auto;   
+--------+---------+----------+----------+   
| autoId | autonum | autotype | garageid |   
+--------+---------+----------+----------+   
|      1 | hk2222  | car      |        1 |   
|      2 | bj0000  | car      |        1 |   
+--------+---------+----------+----------+ 

mysql> select * from auto;
+--------+---------+----------+----------+
| autoId | autonum | autotype | garageid |
+--------+---------+----------+----------+
|      1 | hk2222  | car      |        1 |
|      2 | bj0000  | car      |        1 |
+--------+---------+----------+----------+


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭