Foreign-key constaint violation upon deletion with cascade remove

Description

When having the following entity structure:
A <--> B (One-To-One)
A <--> C (One-To-One)
B <-=> C (One-To-Many)
With CascadeType.ALL from A to B, A to C and B to C.

Having the following instances:
a1->b->c
a2->c

When deleting b (by ID) and then c (by ID) you get a foreign key constraint violation, stating that b is still referenced by c (even though there is a Cascade that should take place).

Notice that deleting it not by ID doesn't cause the issue, as then Spring doesn't call entityManager.exist which in turn doesn't do an autoFlush (which seems to be the problem).

The test case reproduces it in a simple manner, even though it uses Spring.

Attachments

1

Activity

Show:

Christian Beikov February 18, 2025 at 2:43 PM

It seems like there is a wrong expectation here. I reproduced this, but figured that the user code is simply wrong, because it fails to maintain both sides of the association. You can’t have an entity pointing to a removed entity through a cascaded association. You have to remove all the association references in the object model. See the test I wrote:

Steve Ebersole October 28, 2015 at 3:25 AM

As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.

Steve Ebersole October 27, 2015 at 7:16 PM

This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.

For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/

Incomplete

Details

Assignee

Reporter

Labels

Affects versions

Priority

Created April 2, 2013 at 12:11 PM
Updated February 18, 2025 at 2:43 PM
Resolved February 18, 2025 at 2:43 PM