jpa级联删除(CascadeType.REMOVE)

Garage.java
Java代码   收藏代码
  1. package com.hibernate.jpa.bean1;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. import javax.persistence.CascadeType;
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.FetchType;
  8. import javax.persistence.GeneratedValue;
  9. import javax.persistence.Id;
  10. import javax.persistence.OneToMany;
  11. @Entity
  12. public class Garage {
  13.     /**
  14.      * many to one 多对一
  15.      */
  16.     private Integer gid;
  17.     private String garagenum;
  18.     private Set<Auto> autos = new HashSet<Auto>();
  19.     @Id @GeneratedValue
  20.     public Integer getGid() {
  21.         return gid;
  22.     }
  23.     public void setGid(Integer gid) {
  24.         this.gid = gid;
  25.     }
  26.     @Column (length=20)
  27.     public String getGaragenum() {
  28.         return garagenum;
  29.     }
  30.     public void setGaragenum(String garagenum) {
  31.         this.garagenum = garagenum;
  32.     }
  33.     @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")
  34.     public Set<Auto> getAutos() {
  35.         return autos;
  36.     }
  37.     public void setAutos(Set<Auto> autos) {
  38.         this.autos = autos;
  39.     }
  40.     public void addGarageAuto(Auto auto) {
  41.         auto.setGarage(this);
  42.         this.autos.add(auto);
  43.     }
  44. }
Auto.java
Java代码   收藏代码
  1. package com.hibernate.jpa.bean1;
  2. import javax.persistence.CascadeType;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.Id;
  6. import javax.persistence.JoinColumn;
  7. import javax.persistence.ManyToOne;
  8. @Entity
  9. public class Auto {
  10.     /**
  11.      * one to many 一对多关联
  12.      */
  13.     private Integer autoId;
  14.     private String autotype;
  15.     private String autonum;
  16.     private Garage garage;
  17.     @Id @GeneratedValue
  18.     public Integer getAutoId() {
  19.         return autoId;
  20.     }
  21.     public void setAutoId(Integer autoId) {
  22.         this.autoId = autoId;
  23.     }
  24.     public String getAutotype() {
  25.         return autotype;
  26.     }
  27.     public void setAutotype(String autotype) {
  28.         this.autotype = autotype;
  29.     }
  30.     public String getAutonum() {
  31.         return autonum;
  32.     }
  33.     public void setAutonum(String autonum) {
  34.         this.autonum = autonum;
  35.     }
  36.     @ManyToOne()
  37.     @JoinColumn(name="garageid")
  38.     public Garage getGarage() {
  39.         return garage;
  40.     }
  41.     public void setGarage(Garage garage) {
  42.         this.garage = garage;
  43.     }
  44. }
持久化数据 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代码   收藏代码
  1. @Test public void delete() {
  2.     EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");
  3.     EntityManager em = factory.createEntityManager();
  4.     em.getTransaction().begin();
  5.     Garage garage = em.find(Garage.class3);
  6.     em.remove(garage);
  7.     em.getTransaction().commit();
  8.     em.close();
  9.     factory.close();
  10. }
调用delete方法是myeclipse控制台出现异常 javax.persistence.RollbackException: Error while commiting the transaction Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`itcast/auto`, CONSTRAINT `FK1F51CFA8A25FB2` FOREIGN KEY (`garageid`) REFERENCES `garage` (`gid`))   发出的sql语句是:
C#代码   收藏代码
  1. Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?
  (二)在Garage.java中添加CascadeType.REMOVE注解
Java代码   收藏代码
  1. @OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE},mappedBy="garage")
  2. public Set<Auto> getAutos() {
  3.     return autos;
  4. }
此时再次调用junit单元测试的delete方法 测试显示成功,发出的sql语句为
Sql代码   收藏代码
  1. Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?
  2. Hibernate: select autos0_.garageid as garageid1_, autos0_.autoId as autoId1_, autos0_.autoId as autoId0_0_, autos0_.autonum as autonum0_0_, autos0_.autotype as autotype0_0_, autos0_.garageid as garageid0_0_ from Auto autos0_ where autos0_.garageid=?
  3. Hibernate: delete from Auto where autoId=?
  4. Hibernate: delete from Auto where autoId=?
  5. Hibernate: delete from Garage where gid=?
      此时表garage中的gid为3的字段被全部删除,同时也级联删除了与garage相关联表auto中garageid为3的字段
Sql代码   收藏代码
  1. mysql> select * from garage;
  2. +-----+-----------+
  3. | gid | garagenum |
  4. +-----+-----------+
  5. |   1 | room1     |
  6. |   2 | room2     |
  7. +-----+-----------+
 
Sql代码   收藏代码
  1. mysql> select * from auto;
  2. +--------+---------+----------+----------+
  3. | autoId | autonum | autotype | garageid |
  4. +--------+---------+----------+----------+
  5. |      1 | hk2222  | car      |        1 |
  6. |      2 | bj0000  | car      |        1 |
  7. +--------+---------+----------+----------+
怎么样,这下级联删除也明白了吧?

你可能感兴趣的:(jpa级联删除(CascadeType.REMOVE))