jpa级联操作详解3--级联删除(2)(CascadeType.REMOVE)

在上一讲中jpa级联操作详解2 Garage.java中有一个CascadeType.REMOVE注解,是在删除garage表中数据的时候级联删除auto表中的数据;这次我们研究在Auto中有一个CascadeType.REMOVE注解时,在删除auto表中的数据的时候能否级联删除garage表中的数据

(一)不在Auto.java添加CascadeType.REMOVE注解时

数据库中的数据如下

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

运行单元测试方法 delete2()

	@Test public void delete2() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Auto auto4 = em.find(Auto.class, 1);
		Auto auto5 = em.find(Auto.class, 2);
		em.remove(auto4);
		em.remove(auto5);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

单元测试成功,auto中对应的两条字段被删除

发出的sql语句为:

Hibernate: select auto0_.autoId as autoId0_1_, auto0_.autonum as autonum0_1_, auto0_.autotype as autotype0_1_, auto0_.garageid as garageid0_1_, garage1_.gid as gid1_0_, garage1_.garagenum as garagenum1_0_ from Auto auto0_ left outer join Garage garage1_ on auto0_.garageid=garage1_.gid where auto0_.autoId=?
Hibernate: select auto0_.autoId as autoId0_1_, auto0_.autonum as autonum0_1_, auto0_.autotype as autotype0_1_, auto0_.garageid as garageid0_1_, garage1_.gid as gid1_0_, garage1_.garagenum as garagenum1_0_ from Auto auto0_ left outer join Garage garage1_ on auto0_.garageid=garage1_.gid where auto0_.autoId=?
Hibernate: delete from Auto where autoId=?
Hibernate: delete from Auto where autoId=?

虽然auto中对应的garage字段被删除,但是garage字段gid=1时虽然在auto表中没有了对应的记录,但是这条数据依然存在

mysql> select * from garage;
+-----+-----------+
| gid | garagenum |
+-----+-----------+
| 1 | room1 |
+-----+-----------+

-----------------------------------------

(二)在Auto.java中加入CascadeType.REMOVE字段

	@ManyToOne(cascade={CascadeType.REMOVE})
	@JoinColumn(name="garageid")
	public Garage getGarage() {
		return garage;
	}

复原数据库中的数据为

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

重新运行delete2()方法,测试成功

打开数据库,数据显示auto表和garage表中的相关数据都被删除了

mysql> select * from auto;
Empty set (0.00 sec)
mysql> select * from garage;
Empty set (0.00 sec)

回看一下这次发出的sql语句为

Hibernate: select auto0_.autoId as autoId0_1_, auto0_.autonum as autonum0_1_, auto0_.autotype as autotype0_1_, auto0_.garageid as garageid0_1_, garage1_.gid as gid1_0_, garage1_.garagenum as garagenum1_0_ from Auto auto0_ left outer join Garage garage1_ on auto0_.garageid=garage1_.gid where auto0_.autoId=?
Hibernate: select auto0_.autoId as autoId0_1_, auto0_.autonum as autonum0_1_, auto0_.autotype as autotype0_1_, auto0_.garageid as garageid0_1_, garage1_.gid as gid1_0_, garage1_.garagenum as garagenum1_0_ from Auto auto0_ left outer join Garage garage1_ on auto0_.garageid=garage1_.gid where auto0_.autoId=?
Hibernate: update Auto set autonum=?, autotype=?, garageid=? where autoId=?
Hibernate: delete from Auto where autoId=?
Hibernate: delete from Garage where gid=?
Hibernate: delete from Auto where autoId=?
 

注意 :如果delete2方法中改为:只删除一条记录,这样就会出错

	@Test public void delete2() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Auto auto4 = em.find(Auto.class, 1);
		em.remove(auto4);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

 观察发出的sql语句

Hibernate: select auto0_.autoId as autoId0_1_, auto0_.autonum as autonum0_1_, auto0_.autotype as autotype0_1_, auto0_.garageid as garageid0_1_, garage1_.gid as gid1_0_, garage1_.garagenum as garagenum1_0_ from Auto auto0_ left outer join Garage garage1_ on auto0_.garageid=garage1_.gid where auto0_.autoId=?
Hibernate: delete from Auto where autoId=?
Hibernate: delete from Garage where gid=?

 我可以发现由于数据auto中有两条记录,先输出了id为1的字段记录后,由于又要删除garage中的相应记录

delete from Garage where gid=?

但是由于还有一个外键关联相应的garage gid=1的记录,所以删除会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值