I have an entity with a collection of basic elements mapped with @ElementCollection:
A bulk delete on this entity fails due to constraint violation on the collection table (Hibernate ignores the collection table).
I don't think that the following cascading restriction from the JPA 2.0 specification applies:
4.10 Bulk Update and Delete Operations
A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.
In other words, Hibernate should handle the collection table as we have no way to bulk delete the basic elements from the collection table.
I have attached a test case allowing to reproduce the issue.
Hibernate 3.5.x, all database, all platforms.
Well, the JPA 2.0 spec simply doesn't forbid this use case (a collection of items isn't a collection of entity), so support is implied and I just thought that qualifying the problem as a "Bug" was appropriate. Apologies for not using "New Feature".
Just for clarification. Does this mean when using hibernate, the only way to delete objects that use an @ElementCollection annotation must be deleted one by one. ie by doing a query and then individually deleting each object?
Specs by nature are inclusive of what they support, the set of features they do not support being much larger.
And regardless I am talking about the Hibernate documentation, which explicitly states this will not happen.
The same question holds for the association table for many-to-many associations.
James, no. You could also (a) clean up the collection table yourself or (b) use cascading foreign keys in the schema.
I am having this exact same problem! Is there a work around? I cant work out a way to solve this problem without manually pulling out each object from the database and removing each object from the element collection!
As the JPA wikibook explains : "There is no cascade option on an ElementCollection, the target objects are always persisted, merged, removed with their parent." Embeddable entities and Basic types should have the same lifetime as their owner, effectively all operations should cascade.