-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HHH-8114 Add test for merging detached collection with removed element #9625
Open
beikov
wants to merge
1
commit into
hibernate:main
Choose a base branch
from
beikov:HHH-8114
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
...e/src/test/java/org/hibernate/orm/test/cascade/MergeCollectionWithRemovedElementTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.cascade; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import org.hibernate.Hibernate; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.Jira; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@DomainModel( | ||
annotatedClasses = { | ||
MergeCollectionWithRemovedElementTest.Parent.class, | ||
MergeCollectionWithRemovedElementTest.Child.class, | ||
MergeCollectionWithRemovedElementTest.GrandChild.class | ||
} | ||
) | ||
@SessionFactory | ||
@Jira("https://hibernate.atlassian.net/browse/HHH-8114") | ||
public class MergeCollectionWithRemovedElementTest { | ||
|
||
@BeforeEach | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
Parent p1 = new Parent( 1L, "p1" ); | ||
Child c1 = new Child( 2L ); | ||
Child c2 = new Child( 3L ); | ||
GrandChild gc1 = new GrandChild( 4L ); | ||
GrandChild gc2 = new GrandChild( 5L ); | ||
GrandChild gc3 = new GrandChild( 6L ); | ||
GrandChild gc4 = new GrandChild( 7L ); | ||
p1.addChild( c1 ); | ||
p1.addChild( c2 ); | ||
c1.addGrandChild( gc1 ); | ||
c1.addGrandChild( gc2 ); | ||
c2.addGrandChild( gc3 ); | ||
c2.addGrandChild( gc4 ); | ||
session.persist( p1 ); | ||
session.persist( gc1 ); | ||
session.persist( gc2 ); | ||
session.persist( gc3 ); | ||
session.persist( gc4 ); | ||
} | ||
); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
session.createQuery( "delete from GrandChild" ).executeUpdate(); | ||
session.createQuery( "delete from Child" ).executeUpdate(); | ||
Check noticeCode scanning / CodeQL Deprecated method or constructor invocation Note test
Invoking
QueryProducerImplementor.createQuery Error loading related location Loading |
||
session.createQuery( "delete from Parent" ).executeUpdate(); | ||
Check noticeCode scanning / CodeQL Deprecated method or constructor invocation Note test
Invoking
QueryProducerImplementor.createQuery Error loading related location Loading |
||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testMergeDetachedAfterFlush(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
// Load all data | ||
Parent parent = session.find( Parent.class, 1L); | ||
for ( Child u : parent.children ) { | ||
Hibernate.initialize( u.grandChildren ); | ||
} | ||
// Detach all data | ||
session.clear(); | ||
// Load the parent into the PC | ||
session.find( Parent.class, 1L); | ||
// Remove a grand child and also remove it from the detached object | ||
Child u = parent.children.iterator().next(); | ||
GrandChild grandChild = u.grandChildren.iterator().next(); | ||
session.remove( grandChild ); | ||
u.grandChildren.remove( grandChild ); | ||
// Flush the remove of the grand child | ||
session.flush(); | ||
// Clear the PC | ||
session.clear(); | ||
// Merge the detached object | ||
session.merge( parent ); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "Parent") | ||
public static class Parent { | ||
@Id | ||
Long id; | ||
String name; | ||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) | ||
Set<Child> children = new HashSet<>(0); | ||
|
||
public Parent() { | ||
} | ||
|
||
public Parent(Long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public void addChild(Child child) { | ||
children.add(child); | ||
child.parent = this; | ||
} | ||
} | ||
|
||
@Entity(name = "Child") | ||
public static class Child { | ||
@Id | ||
Long id; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
Parent parent; | ||
@OneToMany(mappedBy = "child") | ||
Set<GrandChild> grandChildren = new HashSet<>(0); | ||
|
||
public Child() { | ||
} | ||
|
||
public Child(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void addGrandChild(GrandChild grandChild) { | ||
grandChildren.add(grandChild); | ||
grandChild.child = this; | ||
} | ||
} | ||
|
||
@Entity(name = "GrandChild") | ||
public static class GrandChild { | ||
@Id | ||
Long id; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
Child child; | ||
|
||
public GrandChild() { | ||
} | ||
|
||
public GrandChild(Long id) { | ||
this.id = id; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Deprecated method or constructor invocation Note test