views:

1414

answers:

4

Hi, 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.

A: 

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...).

KLE
Hi,I've just added the DELETE_ORPHAN cascade, just to overcome with a solution, but seems that is not the problem. Anyway, i make a simple rest call thru DELETE, send an id of document which will be deleted, so... a very simple scenario.
Cristian Boariu
according to hibernate docs (lazy to look for it now) the `@Cascade` _adds_ to the JPA `cascade` attribute, doesn't override it.
Bozho
@Bozho Thanks for this precision, in theory ;-). I wish somebody tested this in practice, maybe Cristian?? :-)
KLE
A: 

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...

GreenieMeanie
+1  A: 

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.

Bozho
Hi Bozho. I call remove ONLY to the document, and i was expecting that the rest of the entities would be removed thru cascade. To call document.getDocumentType().getPropertyTypes().remove(propertyType); i need to do a foreach on propertyes set, and when i remove one, an exception is fired because the number of property types does not remain the same...
Cristian Boariu
tripple check whether you aren't calling remove somewhere ;) then see my update
Bozho
A: 

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.
Cristian Boariu
yup - that was hidden from us, because you didn't show it ;)
Bozho