
问题:使用了jpa的@OneToMany注解,当更新子表中的数据时,子表中新增了一条新纪录,并将旧的数据的外键id设置为null,因此产生了孤儿数据,于是查找资料找到了
orphanRemoval设置为true可以删除孤儿数据
添加了orphanRemoval=true又跳进了另一个坑
1 | org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: io.arukas.entity.Post.imgs; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: io.arukas.entity.Post.imgs |
PostImage.java
1 |
|
Post.java
1 |
|
PostService.java
1 |
|
最终执行sql,看sql还是需要优化呢,以后有空解决吧。。。
1 | Hibernate: select post0_.id as id1_2_0_, post0_.title as title2_2_0_, post0_.summary as summary3_2_0_, post0_.content as content4_2_0_, post0_.author_id as author_i8_2_0_, post0_.create_at as create_a5_2_0_, post0_.update_at as update_a6_2_0_, post0_.version as version7_2_0_, (select count(t.id) from comments t where t.post_id = post0_.id) as formula0_0_, user1_.id as id1_7_1_, user1_.username as username2_7_1_, user1_.password as password3_7_1_, user1_.birthday as birthday4_7_1_, user1_.gender as gender5_7_1_, user1_.unionid as unionid6_7_1_, user1_.create_at as create_a7_7_1_, user1_.update_at as update_a8_7_1_, user1_.version as version9_7_1_ from posts post0_ left outer join users user1_ on post0_.author_id=user1_.id where post0_.id=? |