Skip to content

Commit 26e41b9

Browse files
dreab8sebersole
authored andcommitted
HHH-15271 Add test for issue
1 parent 46f5618 commit 26e41b9

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.entitygraph;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.FetchType;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.NamedAttributeNode;
12+
import jakarta.persistence.NamedEntityGraph;
13+
import jakarta.persistence.OneToOne;
14+
import org.hibernate.Hibernate;
15+
import org.hibernate.Session;
16+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
17+
import org.hibernate.testing.orm.junit.JiraKey;
18+
import org.hibernate.testing.orm.junit.Jpa;
19+
import org.junit.jupiter.api.BeforeAll;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.Collections;
23+
24+
import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOAD_GRAPH;
25+
import static org.junit.jupiter.api.Assertions.assertFalse;
26+
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
28+
@Jpa(
29+
annotatedClasses = {
30+
LoadGraphMergeTest.Parent.class,
31+
LoadGraphMergeTest.Child.class,
32+
LoadGraphMergeTest.GrandChild.class,
33+
}
34+
)
35+
@JiraKey( "HHH-15271" )
36+
public class LoadGraphMergeTest {
37+
38+
private static final Long PARENT_ID_1 = 1L;
39+
private static final Long PARENT_ID_2 = 2L;
40+
41+
@BeforeAll
42+
public static void init(EntityManagerFactoryScope scope) {
43+
scope.inTransaction(
44+
entityManager -> {
45+
GrandChild grandChild = new GrandChild( 1L, "grand child 1" );
46+
Child child = new Child( 1L, grandChild );
47+
Parent parent = new Parent( PARENT_ID_1, child );
48+
entityManager.persist( parent );
49+
50+
GrandChild grandChild2 = new GrandChild( 2L, "grand child 2" );
51+
Child child2 = new Child( 2L, grandChild2 );
52+
Parent parent2 = new Parent( PARENT_ID_2, child2 );
53+
entityManager.persist( parent2 );
54+
}
55+
);
56+
}
57+
58+
@Test
59+
public void testGrandChildHasNotBeenInitializedByMerge(EntityManagerFactoryScope scope) {
60+
Parent parent = scope.fromTransaction( entityManager ->
61+
entityManager.find(
62+
Parent.class,
63+
PARENT_ID_1,
64+
Collections.singletonMap(
65+
HINT_SPEC_LOAD_GRAPH,
66+
entityManager.getEntityGraph( "parent.child" ) ) )
67+
);
68+
69+
Parent parent2 = scope.fromTransaction( entityManager ->
70+
entityManager.find(
71+
Parent.class,
72+
PARENT_ID_2,
73+
Collections.singletonMap(
74+
HINT_SPEC_LOAD_GRAPH,
75+
entityManager.getEntityGraph( "parent.child" ) ) )
76+
);
77+
78+
scope.inTransaction( entityManager -> {
79+
assertTrue( Hibernate.isInitialized( parent.getChild() ) );
80+
assertFalse( Hibernate.isInitialized( parent.getChild().getGrandChild() ) );
81+
82+
Session session = entityManager.unwrap( Session.class );
83+
84+
Parent mergedParent = session.merge( parent,
85+
entityManager.getEntityGraph( "parent.child" ) );
86+
87+
Child child = mergedParent.getChild();
88+
assertTrue( Hibernate.isInitialized( child ) );
89+
assertFalse( Hibernate.isInitialized( child.getGrandChild() ),
90+
"Merge has initialized `parent.child` lazy association" );
91+
92+
assertTrue( Hibernate.isInitialized( parent2.getChild() ) );
93+
assertFalse( Hibernate.isInitialized( parent2.getChild().getGrandChild() ) );
94+
95+
Parent mergedParent2 = session.merge( parent2 );
96+
97+
Child child2 = mergedParent2.getChild();
98+
assertTrue( Hibernate.isInitialized( child2 ) );
99+
assertTrue( Hibernate.isInitialized( child2.getGrandChild() ) );
100+
} );
101+
}
102+
103+
@Test
104+
public void testChildHasNotBeenInitializedByMerge(EntityManagerFactoryScope scope) {
105+
Parent parent = scope.fromTransaction( entityManager ->
106+
entityManager.find(
107+
Parent.class,
108+
PARENT_ID_1 )
109+
);
110+
111+
scope.inTransaction( entityManager -> {
112+
Child child1 = parent.getChild();
113+
assertFalse( Hibernate.isInitialized( child1 ) );
114+
115+
Session session = entityManager.unwrap( Session.class );
116+
Parent mergedParent = session.merge( parent, session.createEntityGraph( "parent" ) );
117+
118+
Child child = mergedParent.getChild();
119+
assertFalse( Hibernate.isInitialized( child ),
120+
"Merge has initialized `parent.child` lazy association" );
121+
} );
122+
}
123+
124+
@Entity(name = "Parent")
125+
@NamedEntityGraph(
126+
name = "parent.child",
127+
attributeNodes = @NamedAttributeNode("child")
128+
)
129+
@NamedEntityGraph(
130+
name = "parent"
131+
)
132+
public static class Parent {
133+
134+
@Id
135+
private Long id;
136+
137+
private String name;
138+
139+
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
140+
private Child child;
141+
142+
public Parent() {
143+
}
144+
145+
public Parent(Long id, Child child) {
146+
this.id = id;
147+
this.child = child;
148+
}
149+
150+
public Long getId() {
151+
return id;
152+
}
153+
154+
public Child getChild() {
155+
return child;
156+
}
157+
}
158+
159+
@Entity(name = "Child")
160+
public static class Child {
161+
162+
@Id
163+
private Long id;
164+
165+
private String name;
166+
167+
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
168+
private GrandChild grandChild;
169+
170+
public Child() {
171+
}
172+
173+
public Child(Long id, GrandChild grandChild) {
174+
this.id = id;
175+
this.grandChild = grandChild;
176+
}
177+
178+
179+
public Long getId() {
180+
return id;
181+
}
182+
183+
public GrandChild getGrandChild() {
184+
return grandChild;
185+
}
186+
}
187+
188+
@Entity(name = "GrandChild")
189+
public static class GrandChild {
190+
191+
@Id
192+
private Long id;
193+
194+
private String name;
195+
196+
public GrandChild() {
197+
}
198+
199+
public GrandChild(Long id, String name) {
200+
this.id = id;
201+
this.name = name;
202+
}
203+
204+
public Long getId() {
205+
return id;
206+
}
207+
208+
}
209+
210+
}

0 commit comments

Comments
 (0)