且构网

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

JPA:级联删除不会删除子级

更新时间:2023-02-05 13:54:02

在删除实体之前,我需要刷新该实体:

I needed to refresh the entity before removing it:

em.refresh(sampleContext);
em.remove(sampleContext);

以前,要删除的实体(sampleContext)不知道sampleUser与之关联(可能是因为sampleContext是从缓存中获取的).在delete之前执行refresh可以确保从数据库中更新实体.

Earlier, the entity being deleted (sampleContext) did not know that sampleUser is associated with it (probably because sampleContext was being fetched from cache). Doing a refresh before delete ensures that the entity is updated from the database.