JPA delete all entites works strange

时光总嘲笑我的痴心妄想 提交于 2019-11-28 14:25:47

The following

em.createQuery("DELETE FROM ParentObj po").executeUpdate(); 

is a JPQL bulk update command and as the JPQL language reference notes:

10.2.9. JPQL Bulk Update and Delete

Operations Bulk update and delete operations apply to entities of a single entity class (together with its subclasses, if any).

A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.....

https://docs.oracle.com/html/E24396_01/ejb3_langref.html#ejb3_langref_bulk_ops

So essentially you are attempting to remove an entity which, due to lack of cascading, will still have FK references in the database. Hence the exception.

CriteriaDelete has similar limitations:

https://docs.oracle.com/javaee/7/api/javax/persistence/criteria/CriteriaDelete.html

Criteria API bulk delete operations map directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!