Skip to content

Commit d148d30

Browse files
committed
HHH-15271 Add test for issue
1 parent c959c0b commit d148d30

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
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.Information.class,
31+
LoadGraphMergeTest.File.class,
32+
LoadGraphMergeTest.Content.class,
33+
}
34+
)
35+
@JiraKey( "HHH-15271" )
36+
public class LoadGraphMergeTest {
37+
38+
private static final Long INFORMATION_ID_1 = 1L;
39+
private static final Long INFORMATION_ID_2 = 2L;
40+
41+
@BeforeAll
42+
public static void init(EntityManagerFactoryScope scope) {
43+
scope.inTransaction(
44+
entityManager -> {
45+
Content content = new Content( 1L, "test content" );
46+
File file = new File( 1L, content );
47+
Information information = new Information( INFORMATION_ID_1, file );
48+
entityManager.persist( information );
49+
50+
Content content2 = new Content( 2L, "test content" );
51+
File file2 = new File( 2L, content2 );
52+
Information information2 = new Information( INFORMATION_ID_2, file2 );
53+
entityManager.persist( information2 );
54+
}
55+
);
56+
}
57+
58+
@Test
59+
public void testContentHasNotBeenInitializedByMerge(EntityManagerFactoryScope scope) {
60+
Information information1 = scope.fromTransaction( entityManager ->
61+
entityManager.find(
62+
Information.class,
63+
INFORMATION_ID_1,
64+
Collections.singletonMap(
65+
HINT_SPEC_LOAD_GRAPH,
66+
entityManager.getEntityGraph( "information.file" ) ) )
67+
);
68+
69+
Information information2 = scope.fromTransaction( entityManager ->
70+
entityManager.find(
71+
Information.class,
72+
INFORMATION_ID_2,
73+
Collections.singletonMap(
74+
HINT_SPEC_LOAD_GRAPH,
75+
entityManager.getEntityGraph( "information.file" ) ) )
76+
);
77+
78+
scope.inTransaction( entityManager -> {
79+
assertTrue( Hibernate.isInitialized( information1.getFile() ) );
80+
assertFalse( Hibernate.isInitialized( information1.getFile().getContent() ) );
81+
82+
Session session = entityManager.unwrap( Session.class );
83+
84+
Information mergedInformation = session.merge( information1,
85+
entityManager.getEntityGraph( "information.file" ) );
86+
87+
File file = mergedInformation.getFile();
88+
assertTrue( Hibernate.isInitialized( file ) );
89+
assertFalse( Hibernate.isInitialized( file.getContent() ),
90+
"Merge has initialized `file.content` lazy association" );
91+
92+
assertTrue( Hibernate.isInitialized( information2.getFile() ) );
93+
assertFalse( Hibernate.isInitialized( information2.getFile().getContent() ) );
94+
95+
Information mergedInformation2 = session.merge( information2 );
96+
97+
File file2 = mergedInformation2.getFile();
98+
assertTrue( Hibernate.isInitialized( file2 ) );
99+
assertTrue( Hibernate.isInitialized( file2.getContent() ) );
100+
} );
101+
}
102+
103+
@Test
104+
public void testFileHasNotBeenInitializedByMerge(EntityManagerFactoryScope scope) {
105+
Information information = scope.fromTransaction( entityManager ->
106+
entityManager.find(
107+
Information.class,
108+
INFORMATION_ID_1 )
109+
);
110+
111+
scope.inTransaction( entityManager -> {
112+
File file1 = information.getFile();
113+
assertFalse( Hibernate.isInitialized( file1 ) );
114+
115+
Session session = entityManager.unwrap( Session.class );
116+
Information mergedInformation = session.merge( information, session.createEntityGraph( "information" ) );
117+
118+
File file = mergedInformation.getFile();
119+
assertFalse( Hibernate.isInitialized( file ),
120+
"Merge has initialized `information.file` lazy association" );
121+
} );
122+
}
123+
124+
@Entity(name = "Information")
125+
@NamedEntityGraph(
126+
name = "information.file",
127+
attributeNodes = @NamedAttributeNode("file")
128+
)
129+
@NamedEntityGraph(
130+
name = "information"
131+
)
132+
public static class Information {
133+
134+
@Id
135+
private Long id;
136+
137+
private String info;
138+
139+
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
140+
private File file;
141+
142+
public Information() {
143+
}
144+
145+
public Information(Long id, File file) {
146+
this.id = id;
147+
this.file = file;
148+
}
149+
150+
public File getFile() {
151+
return file;
152+
}
153+
154+
public Long getId() {
155+
return id;
156+
}
157+
}
158+
159+
@Entity(name = "File")
160+
public static class File {
161+
162+
@Id
163+
private Long id;
164+
165+
private String name;
166+
167+
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
168+
private Content content;
169+
170+
public File() {
171+
}
172+
173+
public File(Long id, Content content) {
174+
this.id = id;
175+
this.content = content;
176+
}
177+
178+
public Content getContent() {
179+
return content;
180+
}
181+
182+
public Long getId() {
183+
return id;
184+
}
185+
}
186+
187+
@Entity(name = "Content")
188+
public static class Content {
189+
190+
@Id
191+
private Long id;
192+
193+
private String content;
194+
195+
public Content() {
196+
}
197+
198+
public Content(Long id, String content) {
199+
this.id = id;
200+
this.content = content;
201+
}
202+
203+
public Long getId() {
204+
return id;
205+
}
206+
207+
public String getContent() {
208+
return content;
209+
}
210+
}
211+
212+
}

0 commit comments

Comments
 (0)