且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Spring Data JPA中删除相同事务后插入

更新时间:2023-01-31 22:57:51

休眠更改了命令的顺序.它按以下顺序工作: 以特殊顺序执行所有SQL和二级缓存更新,以免违反外键约束:

Hibernate changed order of the commands. It works in below order : Execute all SQL and second-level cache updates, in a special order so that foreign-key constraints cannot be violated:

    1. Inserts, in the order they were performed
    2. Updates
    3. Deletion of collection elements
    4. Insertion of collection elements
    5. Deletes, in the order they were performed 

确实是这样.刷新时,Hibernate在delete语句之前执行所有插入操作. 可能的选项是:
1.删​​除后立即显式调用entityManager.flush(). 或者 2.尽可能更新现有行,然后从其余位置创建ToBeDeleted List.这样可以确保使用新值更新现有记录,并保存全新的记录.

And that is exactly the case. When flushing, Hibernate executes all inserts before delete statements. The possible option are :
1. To call entityManager.flush() explicitly just after the delete. OR 2. Wherever possible update existing rows and from rest create ToBeDeleted List. This will ensure that existing records are updated with new values and completely new records are saved.