Bulk delete on Entity with @ElementCollection ignores the @CollectionTable

Description

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.

Environment

Hibernate 3.5.x, all database, all platforms.

Activity

Show:
PascalP
September 3, 2010, 5:55 AM
Edited

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

JacobR
September 3, 2010, 6:21 AM

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?

Steve Ebersole
September 3, 2010, 1:14 PM

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.

Ling Lin
October 11, 2010, 4:45 AM

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!

James Howe
January 17, 2011, 1:43 PM

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.

Assignee

Unassigned

Reporter

PascalP

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure