|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.fetch.runtime.managed; |
6 | 6 |
|
7 | | -import java.util.Collections; |
8 | 7 | import jakarta.persistence.Entity; |
9 | 8 | import jakarta.persistence.FetchType; |
10 | 9 | import jakarta.persistence.Id; |
11 | 10 | import jakarta.persistence.JoinColumn; |
12 | 11 | import jakarta.persistence.ManyToOne; |
13 | 12 | import jakarta.persistence.Table; |
14 | | - |
15 | 13 | import org.hibernate.Hibernate; |
16 | | -import org.hibernate.boot.MetadataSources; |
17 | | -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
18 | | -import org.hibernate.cfg.AvailableSettings; |
19 | 14 | import org.hibernate.graph.spi.RootGraphImplementor; |
20 | 15 | import org.hibernate.query.Query; |
21 | | - |
22 | | -import org.hibernate.testing.FailureExpected; |
| 16 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 17 | +import org.hibernate.testing.orm.junit.FailureExpected; |
23 | 18 | import org.hibernate.testing.orm.junit.JiraKey; |
24 | | -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
25 | | -import org.junit.After; |
26 | | -import org.junit.Before; |
27 | | -import org.junit.Test; |
| 19 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 22 | +import org.hibernate.testing.orm.junit.Setting; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import java.util.Collections; |
28 | 28 |
|
29 | 29 | import static org.hamcrest.CoreMatchers.is; |
30 | 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.hibernate.cfg.CacheSettings.USE_SECOND_LEVEL_CACHE; |
31 | 32 |
|
32 | 33 | /** |
33 | 34 | * @author Steve Ebersole |
34 | 35 | */ |
35 | | -@JiraKey( value = "HHH-13152" ) |
36 | | -public class RuntimeFetchFromManagedTest extends BaseNonConfigCoreFunctionalTestCase { |
| 36 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 37 | +@JiraKey( "HHH-13152" ) |
| 38 | +@ServiceRegistry(settings = @Setting(name = USE_SECOND_LEVEL_CACHE, value = "false")) |
| 39 | +@DomainModel(annotatedClasses = {RuntimeFetchFromManagedTest.RootEntity.class, RuntimeFetchFromManagedTest.ChildEntity.class}) |
| 40 | +@SessionFactory |
| 41 | +public class RuntimeFetchFromManagedTest { |
37 | 42 |
|
38 | 43 | @Test |
39 | | - public void testFetchingFromManagedEntityHql() { |
40 | | - inTransaction( |
41 | | - session -> { |
42 | | - { |
43 | | - // let's load the root - because the link to child is lazy, this should |
44 | | - // not load it |
45 | | - final RootEntity rootEntity = session.get( RootEntity.class, 2 ); |
46 | | - assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
47 | | - assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) ); |
48 | | - } |
49 | | - |
50 | | - { |
51 | | - // now try to perform an HQL join fetch |
52 | | - final RootEntity rootEntity = session.createQuery( |
53 | | - "select r from RootEntity r join fetch r.child", |
54 | | - RootEntity.class |
55 | | - ).uniqueResult(); |
56 | | - assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
57 | | - assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) ); |
58 | | - } |
59 | | - } |
60 | | - ); |
| 44 | + public void testFetchingFromManagedEntityHql(SessionFactoryScope factoryScope) { |
| 45 | + factoryScope.inTransaction(session -> { |
| 46 | + { |
| 47 | + // let's load the root - because the link to child is lazy, this should |
| 48 | + // not load it |
| 49 | + final RootEntity rootEntity = session.get( RootEntity.class, 2 ); |
| 50 | + assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
| 51 | + assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) ); |
| 52 | + } |
| 53 | + |
| 54 | + { |
| 55 | + // now try to perform an HQL join fetch |
| 56 | + final RootEntity rootEntity = session.createQuery( |
| 57 | + "select r from RootEntity r join fetch r.child", |
| 58 | + RootEntity.class |
| 59 | + ).uniqueResult(); |
| 60 | + assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
| 61 | + assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) ); |
| 62 | + } |
| 63 | + } ); |
61 | 64 | } |
62 | 65 |
|
63 | 66 | @Test |
64 | | - @FailureExpected( jiraKey = "", message = "The entity is returned directly from the PC" ) |
65 | | - public void testFetchingFromManagedEntityEntityGraphLoad() { |
66 | | - inTransaction( |
67 | | - session -> { |
68 | | - { |
69 | | - // let's load the root - because the link to child is lazy, this should |
70 | | - // not load it |
71 | | - final RootEntity rootEntity = session.get( RootEntity.class, 2 ); |
72 | | - assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
73 | | - assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) ); |
74 | | - } |
75 | | - |
76 | | - { |
77 | | - // now try to load the root entity again using an EntityGraph that specifies to fetch child |
78 | | - final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class ); |
79 | | - entityGraph.addAttributeNode( "child" ); |
80 | | - |
81 | | - final RootEntity rootEntity = session.find( |
82 | | - RootEntity.class, |
83 | | - 2, |
84 | | - Collections.singletonMap( "javax.persistence.loadgraph", entityGraph ) |
85 | | - ); |
86 | | - assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
87 | | - assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) ); |
88 | | - } |
89 | | - } |
90 | | - ); |
| 67 | + @FailureExpected(reason = "The entity is returned directly from the PC" ) |
| 68 | + public void testFetchingFromManagedEntityEntityGraphLoad(SessionFactoryScope factoryScope) { |
| 69 | + factoryScope.inTransaction(session -> { |
| 70 | + { |
| 71 | + // let's load the root - because the link to child is lazy, this should |
| 72 | + // not load it |
| 73 | + final RootEntity rootEntity = session.get( RootEntity.class, 2 ); |
| 74 | + assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
| 75 | + assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) ); |
| 76 | + } |
| 77 | + |
| 78 | + { |
| 79 | + // now try to load the root entity again using an EntityGraph that specifies to fetch child |
| 80 | + final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class ); |
| 81 | + entityGraph.addAttributeNode( "child" ); |
| 82 | + |
| 83 | + final RootEntity rootEntity = session.find( |
| 84 | + RootEntity.class, |
| 85 | + 2, |
| 86 | + Collections.singletonMap( "javax.persistence.loadgraph", entityGraph ) |
| 87 | + ); |
| 88 | + assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
| 89 | + assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) ); |
| 90 | + } |
| 91 | + } ); |
91 | 92 | } |
92 | 93 |
|
93 | 94 | @Test |
94 | | - public void testFetchingFromManagedEntityEntityGraphHql() { |
95 | | - inTransaction( |
96 | | - session -> { |
97 | | - { |
98 | | - // let's load the root - because the link to child is lazy, this should |
99 | | - // not load it |
100 | | - final RootEntity rootEntity = session.get( RootEntity.class, 2 ); |
101 | | - assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
102 | | - assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) ); |
103 | | - } |
104 | | - |
105 | | - { |
106 | | - // now try to query the root entity again using an EntityGraph that specifies to fetch child |
107 | | - final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class ); |
108 | | - entityGraph.addAttributeNode( "child" ); |
109 | | - |
110 | | - final Query<RootEntity> query = session.createQuery( |
111 | | - "select r from RootEntity r", |
112 | | - RootEntity.class |
113 | | - ); |
114 | | - |
115 | | - final RootEntity rootEntity = query.setHint( "javax.persistence.loadgraph", entityGraph ).uniqueResult(); |
116 | | - assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
117 | | - assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) ); |
118 | | - } |
119 | | - } |
120 | | - ); |
| 95 | + public void testFetchingFromManagedEntityEntityGraphHql(SessionFactoryScope factoryScope) { |
| 96 | + factoryScope.inTransaction(session -> { |
| 97 | + { |
| 98 | + // let's load the root - because the link to child is lazy, this should |
| 99 | + // not load it |
| 100 | + final RootEntity rootEntity = session.get( RootEntity.class, 2 ); |
| 101 | + assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
| 102 | + assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( false ) ); |
| 103 | + } |
| 104 | + |
| 105 | + { |
| 106 | + // now try to query the root entity again using an EntityGraph that specifies to fetch child |
| 107 | + final RootGraphImplementor<RootEntity> entityGraph = session.createEntityGraph( RootEntity.class ); |
| 108 | + entityGraph.addAttributeNode( "child" ); |
| 109 | + |
| 110 | + final Query<RootEntity> query = session.createQuery( |
| 111 | + "select r from RootEntity r", |
| 112 | + RootEntity.class |
| 113 | + ); |
| 114 | + |
| 115 | + final RootEntity rootEntity = query.setHint( "javax.persistence.loadgraph", entityGraph ).uniqueResult(); |
| 116 | + assertThat( Hibernate.isInitialized( rootEntity ), is( true ) ); |
| 117 | + assertThat( Hibernate.isInitialized( rootEntity.getChild() ), is( true ) ); |
| 118 | + } |
| 119 | + } ); |
121 | 120 | } |
122 | 121 |
|
123 | | - @Before |
124 | | - public void createTestData() { |
| 122 | + @BeforeEach |
| 123 | + public void createTestData(SessionFactoryScope factoryScope) { |
125 | 124 | // create some test |
126 | | - inTransaction( |
127 | | - session -> { |
128 | | - final ChildEntity child = new ChildEntity( 1, "child" ); |
129 | | - session.persist( child ); |
130 | | - |
131 | | - final RootEntity root = new RootEntity( 2, "root", child ); |
132 | | - session.persist( root ); |
133 | | - } |
134 | | - ); |
135 | | - } |
136 | | - |
137 | | - @After |
138 | | - public void cleanUpTestData() { |
139 | | - inTransaction( |
140 | | - session -> { |
141 | | - session.createQuery( "delete from RootEntity" ).executeUpdate(); |
142 | | - session.createQuery( "delete from ChildEntity" ).executeUpdate(); |
143 | | - } |
144 | | - ); |
145 | | - } |
| 125 | + factoryScope.inTransaction(session -> { |
| 126 | + final ChildEntity child = new ChildEntity( 1, "child" ); |
| 127 | + session.persist( child ); |
146 | 128 |
|
147 | | - @Override |
148 | | - protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { |
149 | | - super.configureStandardServiceRegistryBuilder( ssrb ); |
150 | | - ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" ); |
| 129 | + final RootEntity root = new RootEntity( 2, "root", child ); |
| 130 | + session.persist( root ); |
| 131 | + } ); |
151 | 132 | } |
152 | 133 |
|
153 | | - @Override |
154 | | - protected void applyMetadataSources(MetadataSources sources) { |
155 | | - super.applyMetadataSources( sources ); |
156 | | - sources.addAnnotatedClass( RootEntity.class ); |
157 | | - sources.addAnnotatedClass( ChildEntity.class ); |
| 134 | + @AfterEach |
| 135 | + public void cleanUpTestData(SessionFactoryScope factoryScope) { |
| 136 | + factoryScope.dropData(); |
158 | 137 | } |
159 | 138 |
|
160 | 139 | @Entity( name = "RootEntity" ) |
|
0 commit comments