Deleted entity passed to persist exception

前端 未结 4 1006
情歌与酒
情歌与酒 2021-02-08 13:57

I have this kind of entities:

Document | n .. to ..1 | DocumentType | 1 .. to .. n | PropertyType | 1 .. to

相关标签:
4条回答
  • 2021-02-08 14:33

    Solution:

    • There was a CascadeType.REMOVE in a @ManyToOne relationship ! Removed it.

    Why this solution?

    • if you want to delete a child you SURELY do not want to delete its parent because there can be other children related to that parent.
    0 讨论(0)
  • 2021-02-08 14:39

    I see you are setting the cascade in two places : the @OneToMany and the @Cascade. I think this can be a problem, if one overrides the other ...


    The error you are reporting need some more context to be understandable. "Deleting an already deleted entity" involves clearly two operations .... You need to give details about the state before, the operations and the state afterwards (with "state", I mean the state in the database...).

    0 讨论(0)
  • 2021-02-08 14:42

    I assume you have called remove() on an of type PropertyType before. Call remove() only for the "root" entity, and remove the others with something like:

    document.getDocumentType().getPropertyTypes().remove(propertyType);
    

    And retain the DELETE_ORPHAN

    You can then, after verifying you haven't manually called remove() on other entities, try calling:

    document = entityManager.merge(document);
    entityManager.remove(document);
    

    so that the EntityManager reassociates the object with the session first.

    0 讨论(0)
  • 2021-02-08 14:53

    It seems like the Cascade options are somewhere making the Entity Manager think that this object or some other object in the chain needs to be persisted when you call em.remove(document). Need more specifics...

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