Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleted entity passed to persist exception

I have this kind of entities:

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

I simply try to remove a document like: entityManager.remove(document);

but an error is firing:

16:45:51,499 ERROR [[Seam Resource Servlet]] Servlet.service() for servlet Seam Resource Servlet threw exception javax.persistence.EntityNotFoundException: deleted entity passed to persist: [up.docstore.PropertyType#]

The problem seems to come from here:

@OneToMany(mappedBy = "documentType", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@ForeignKey(name = "FK_DOCUMENT_TYPE__PROPERTY_TYPE")
@Sort(type = SortType.NATURAL)
private SortedSet<PropertyType> propertyTypes = new TreeSet<PropertyType>();

If i remove CascadeType.PERSIST all it's working. But i need it there and also i need it EAGERLY.

Does anyone know other solution?

Edit: removed DELETE_ORPHAN cascade, but still the same problem.

like image 693
Cristian Boariu Avatar asked Feb 01 '10 16:02

Cristian Boariu


2 Answers

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.
like image 194
Cristian Boariu Avatar answered Sep 23 '22 07:09

Cristian Boariu


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.

like image 26
Bozho Avatar answered Sep 23 '22 07:09

Bozho