Skip to content

Bidirectional Relationship Persistence - orphanRemoval not working as expected #47006

Open
@yankydoo

Description

@yankydoo

Describe the bug

When clearing collections on both sides of a bidirectional relationship within the same transaction without an intermediate persistAndFlush(), relationship entities are not properly removed from the database.

ProjectEntity has

    @OneToMany(mappedBy = "id.sourceProject", cascade = CascadeType.ALL, orphanRemoval = true)
    List<ProjectRelationship> outgoingRelationships = new ArrayList<>();
    @OneToMany(mappedBy = "id.targetProject", cascade = CascadeType.ALL, orphanRemoval = true)
    List<ProjectRelationship> incomingRelationships = new ArrayList<>();

after creating three projects and connecting them (pe1 -> pe2 -> pe3) I use the following code to disconnect pe2 from pe1 and pe3:

        ProjectEntity project = ProjectEntity.findById(pe2Id);
        project.outgoingRelationships.clear();
        // without the following line, it doesnt work
        // project.persistAndFlush();
        project.incomingRelationships.clear();
        project.persist();

Expected behavior

Clearing collections on both sides of a bidirectional relationship within the same transaction without an intermediate persistAndFlush(), should properly remove entities from the database.

Actual behavior

It seems that only for the collection that was cleared last (incomingRelations) the orphanRemoval works as expected.

How to Reproduce?

I created a repo to reproduce the bug: https://github.com/yankydoo/repro-hibernate-extended-many-to-many-orphan-removal/tree/main

Execute the only test to reproduce it

Output of uname -a or ver

No response

Output of java -version

Java version: 21.0.6, vendor: Amazon.com Inc., runtime: /Users/.../Library/Java/JavaVirtualMachines/corretto-21.0.6/Contents/Home

Quarkus version or git rev

3.19.4

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937

Additional information

No response

Metadata

Metadata

Assignees

Labels

area/hibernate-ormHibernate ORMkind/bug-thirdpartyBugs that are caused by third-party components and not causing a major dysfunction of core Quarkus.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions