@OnDelete(action=CASCADE) with @ElementCollection and @ManyToMany

Description

Allowing @OnDelete(action=CASCADE) with @ElementCollection and @ManyToMany allows us to optimize away the SQL delete statement which would otherwise be needed to remove the collection when its owner is remove()d.

The original issue description is long-obsolete, since Hibernate always cleans up @ElementCollections when the owner is deleted, except when you use a native SQL query. And this has been true for a very long time.

Original issue description below.


Consider the following constellation:

When I persist a Bar object that contains some Foos, and when I later decide to delete the Bar object, I get a foreign key constraint violation error message because the Foo objects, which contain a reference to the Bar object that owns them, are not deleted automatically. I would expect the default behaviour to be "on delete cascade" because the Foo objects are embeddables, thus they cannot exist on their own without the Bar object that owns them. But there is no way to tell Hibernate to cascade:

  • the @OnDelete annotation is only allowed for OneToMany relations

  • the @Cascade annotation is being ignored

  • there is no "cascade" property for the CollectionOfElements

So my question is: Why does the cascading not occur automatically, and how do I tell Hibernate to cascade anyways?

Thanks a lot in advance,

Nicole

Activity

Show:

Gavin KingMay 14, 2024 at 12:45 PM

Done.

Ptits de BarbeSeptember 11, 2023 at 11:25 PM

(Sorry, didn’t notice discussion starting here) My two cents:

  1. Reuse this issue to “allow @OnDelete for @ElementCollection".

  2. Create a new one to “also set things up so that Hibernate doesn’t redundantly execute a SQL DELETE statement to remove the collection”, as this, I believe, requires more work to validate that the actual FK matches the mapping (if Hibernate is asked to validate, of course).

Gavin KingAugust 30, 2023 at 8:21 AM

Can we just rename the issue, since it’s still a totally legit feature request?

Christian BeikovAugust 30, 2023 at 7:02 AM

I’m inclined to close the issue because the @CollectionOfElements annotation is gone now and element collections are always cleaned up automatically by Hibernate on delete of the owner. If the request is to support @OnDelete for @ElementCollection, we should IMO create a new Jira for this feature request.

Gavin KingAugust 27, 2023 at 8:50 AM

This is a great point: we should totally allow @OnDelete for @ElementCollection.

But it’s a bit more involved than just suppressing that (confusing) error message: we need to also set things up so that Hibernate doesn’t redundantly execute a SQL DELETE statement to remove the collection.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Priority

Created June 3, 2008 at 1:15 PM
Updated June 14, 2024 at 3:07 PM
Resolved May 14, 2024 at 12:46 PM