JPA delete all entites works strange

前端 未结 1 1105
予麋鹿
予麋鹿 2020-12-12 04:15

I have an entityrelation like this:

In the ParentObj class:

@OneToMany(mappedBy = \"parentObj\", fetch = FetchType.EAGER, cascade = CascadeType.ALL,          


        
相关标签:
1条回答
  • 2020-12-12 05:15

    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.

    0 讨论(0)
提交回复
热议问题