|
6 | 6 |
|
7 | 7 | import jakarta.persistence.*;
|
8 | 8 | import org.hibernate.annotations.NaturalId;
|
| 9 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
9 | 10 | import org.hibernate.graph.GraphSemantic;
|
10 | 11 | import org.hibernate.graph.RootGraph;
|
11 | 12 | import org.hibernate.testing.orm.junit.DomainModel;
|
@@ -57,6 +58,74 @@ public class NewGraphTest {
|
57 | 58 | assertTrue( isInitialized( ee.f ) );
|
58 | 59 | }
|
59 | 60 |
|
| 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 | + |
60 | 129 | @Test void testBySimpleNaturalIdEntityGraph(SessionFactoryScope scope) {
|
61 | 130 | scope.inTransaction( s-> {
|
62 | 131 | G g = new G();
|
|
0 commit comments