Skip to content

Commit 792c8ab

Browse files
committed
HHH-19298 add tests for find() and get() with EntityGraph
1 parent 0840b5a commit 792c8ab

File tree

1 file changed

+69
-0
lines changed
  • hibernate-core/src/test/java/org/hibernate/orm/test/annotations/fetchprofile

1 file changed

+69
-0
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/fetchprofile/NewGraphTest.java

+69
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import jakarta.persistence.*;
88
import org.hibernate.annotations.NaturalId;
9+
import org.hibernate.engine.spi.SessionFactoryImplementor;
910
import org.hibernate.graph.GraphSemantic;
1011
import org.hibernate.graph.RootGraph;
1112
import org.hibernate.testing.orm.junit.DomainModel;
@@ -57,6 +58,74 @@ public class NewGraphTest {
5758
assertTrue( isInitialized( ee.f ) );
5859
}
5960

61+
@Test void testFind(SessionFactoryScope scope) {
62+
scope.inTransaction( s-> {
63+
G g = new G();
64+
F f = new F();
65+
E e = new E();
66+
f.g = g;
67+
e.f = f;
68+
s.persist(g);
69+
s.persist(f);
70+
s.persist(e);
71+
});
72+
73+
F f = scope.fromSession( s -> s.find(F.class, 1) );
74+
assertFalse( isInitialized( f.g ) );
75+
assertFalse( isInitialized( f.es ) );
76+
F ff = scope.fromSession( s -> {
77+
RootGraph<F> graph = s.createEntityGraph(F.class);
78+
graph.addAttributeNodes("g", "es");
79+
return s.find(graph, 1);
80+
});
81+
assertTrue( isInitialized( ff.g ) );
82+
assertTrue( isInitialized( ff.es ) );
83+
84+
E e = scope.fromSession( s -> s.find(E.class, 1) );
85+
assertFalse( isInitialized( e.f ) );
86+
E ee = scope.fromSession( s -> {
87+
RootGraph<E> graph = s.createEntityGraph(E.class);
88+
graph.addAttributeNodes("f");
89+
return s.find(graph, 1);
90+
});
91+
assertTrue( isInitialized( ee.f ) );
92+
}
93+
94+
@Test void testGet(SessionFactoryScope scope) {
95+
scope.inTransaction( s-> {
96+
G g = new G();
97+
F f = new F();
98+
E e = new E();
99+
f.g = g;
100+
e.f = f;
101+
s.persist(g);
102+
s.persist(f);
103+
s.persist(e);
104+
});
105+
106+
SessionFactoryImplementor factory = scope.getSessionFactory();
107+
108+
F f = factory.fromStatelessSession( s -> s.get(F.class, 1L) );
109+
assertFalse( isInitialized( f.g ) );
110+
assertFalse( isInitialized( f.es ) );
111+
F ff = factory.fromStatelessSession( s -> {
112+
RootGraph<F> graph = s.createEntityGraph(F.class);
113+
graph.addAttributeNodes("g", "es");
114+
return s.get(graph, 1L);
115+
});
116+
assertTrue( isInitialized( ff.g ) );
117+
assertTrue( isInitialized( ff.es ) );
118+
119+
E e = factory.fromStatelessSession( s -> s.get(E.class, 1L) );
120+
assertFalse( isInitialized( e.f ) );
121+
E ee = factory.fromStatelessSession( s -> {
122+
RootGraph<E> graph = s.createEntityGraph(E.class);
123+
graph.addAttributeNodes("f");
124+
return s.get(graph, 1L);
125+
});
126+
assertTrue( isInitialized( ee.f ) );
127+
}
128+
60129
@Test void testBySimpleNaturalIdEntityGraph(SessionFactoryScope scope) {
61130
scope.inTransaction( s-> {
62131
G g = new G();

0 commit comments

Comments
 (0)