8
8
import jakarta .persistence .Column ;
9
9
import jakarta .persistence .Entity ;
10
10
import jakarta .persistence .FetchType ;
11
- import jakarta .persistence .GeneratedValue ;
12
11
import jakarta .persistence .Id ;
13
12
import jakarta .persistence .ManyToOne ;
14
-
15
13
import org .hibernate .LazyInitializationException ;
16
14
import org .hibernate .annotations .FetchProfile ;
17
- import org .hibernate .cfg .Configuration ;
18
- import org .hibernate .cfg .Environment ;
19
-
15
+ import org .hibernate .stat .spi .StatisticsImplementor ;
16
+ import org .hibernate .testing .orm .junit .DomainModel ;
20
17
import org .hibernate .testing .orm .junit .JiraKey ;
21
- import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
22
- import org .junit .Test ;
23
-
24
- import static org .hibernate .testing .transaction .TransactionUtil .doInHibernate ;
25
- import static org .junit .Assert .assertEquals ;
26
- import static org .junit .Assert .fail ;
27
-
18
+ import org .hibernate .testing .orm .junit .ServiceRegistry ;
19
+ import org .hibernate .testing .orm .junit .SessionFactory ;
20
+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
21
+ import org .hibernate .testing .orm .junit .Setting ;
22
+ import org .junit .jupiter .api .AfterEach ;
23
+ import org .junit .jupiter .api .BeforeEach ;
24
+ import org .junit .jupiter .api .Test ;
25
+
26
+ import static org .assertj .core .api .Assertions .assertThat ;
27
+ import static org .hibernate .cfg .StatisticsSettings .GENERATE_STATISTICS ;
28
+ import static org .junit .jupiter .api .Assertions .fail ;
29
+
30
+ @ SuppressWarnings ("JUnitMalformedDeclaration" )
28
31
@ JiraKey ( value = "HHH-12297" )
29
- public class EntityLoadedInTwoPhaseLoadTest extends BaseCoreFunctionalTestCase {
30
-
32
+ @ ServiceRegistry (
33
+ settings = @ Setting ( name = GENERATE_STATISTICS , value = "true" )
34
+ )
35
+ @ DomainModel ( annotatedClasses = {
36
+ EntityLoadedInTwoPhaseLoadTest .Start .class ,
37
+ EntityLoadedInTwoPhaseLoadTest .Mid .class ,
38
+ EntityLoadedInTwoPhaseLoadTest .Finish .class ,
39
+ EntityLoadedInTwoPhaseLoadTest .Via1 .class ,
40
+ EntityLoadedInTwoPhaseLoadTest .Via2 .class
41
+ } )
42
+ @ SessionFactory
43
+ public class EntityLoadedInTwoPhaseLoadTest {
31
44
static final String FETCH_PROFILE_NAME = "fp1" ;
32
45
33
- public void configure (Configuration cfg ) {
34
- cfg .setProperty ( Environment .GENERATE_STATISTICS , true );
35
- }
36
-
37
46
@ Test
38
- public void testIfAllRelationsAreInitialized () {
39
- long startId = this .createSampleData ();
40
- sessionFactory ().getStatistics ().clear ();
47
+ public void testIfAllRelationsAreInitialized (SessionFactoryScope sessions ) {
48
+ final StatisticsImplementor statistics = sessions .getSessionFactory ().getStatistics ();
49
+ statistics .clear ();
50
+
51
+ final Start start = sessions .fromTransaction ( (session ) -> {
52
+ session .enableFetchProfile ( FETCH_PROFILE_NAME );
53
+ return session .find ( Start .class , 1 );
54
+ } );
55
+
56
+ // should have loaded all the data
57
+ assertThat ( statistics .getEntityLoadCount () ).isEqualTo ( 4 );
58
+ // should have loaded it in one query (join fetch)
59
+ assertThat ( statistics .getPrepareStatementCount () ).isEqualTo ( 1 );
60
+
41
61
try {
42
- Start start = this .loadStartWithFetchProfile ( startId );
43
- @ SuppressWarnings ( "unused" )
44
- String value = start .getVia2 ().getMid ().getFinish ().getValue ();
45
- assertEquals ( 4 , sessionFactory ().getStatistics ().getEntityLoadCount () );
62
+ // access the data which was supposed to have been fetched
63
+ //noinspection ResultOfMethodCallIgnored
64
+ start .getVia2 ().getMid ().getFinish ().getValue ();
46
65
}
47
66
catch (LazyInitializationException e ) {
48
67
fail ( "Everything should be initialized" );
49
68
}
50
69
}
51
70
52
- public Start loadStartWithFetchProfile (long startId ) {
53
- return doInHibernate ( this ::sessionFactory , session -> {
54
- session .enableFetchProfile ( FETCH_PROFILE_NAME );
55
- return session .get ( Start .class , startId );
56
- } );
57
- }
58
-
59
- private long createSampleData () {
60
- return doInHibernate ( this ::sessionFactory , session -> {
61
- Finish finish = new Finish ( "foo" );
62
- Mid mid = new Mid ( finish );
63
- Via2 via2 = new Via2 ( mid );
64
- Start start = new Start ( null , via2 );
71
+ @ BeforeEach
72
+ void createTestData (SessionFactoryScope sessions ) {
73
+ sessions .inTransaction ( (session ) -> {
74
+ Finish finish = new Finish ( 1 , "foo" );
75
+ Mid mid = new Mid ( 1 , finish );
76
+ Via2 via2 = new Via2 ( 1 , mid );
77
+ Start start = new Start ( 1 , null , via2 );
65
78
66
79
session .persist ( start );
67
-
68
- return start .getId ();
69
80
} );
70
81
}
71
82
72
- @ Override
73
- protected Class [] getAnnotatedClasses () {
74
- return new Class [] {
75
- Start .class ,
76
- Mid .class ,
77
- Finish .class ,
78
- Via1 .class ,
79
- Via2 .class
80
- };
83
+ @ AfterEach
84
+ void dropTestData (SessionFactoryScope sessions ) {
85
+ sessions .dropData ();
81
86
}
82
87
83
88
@ Entity (name = "FinishEntity" )
84
89
public static class Finish {
85
-
86
90
@ Id
87
- @ GeneratedValue
88
- private long id ;
89
-
91
+ private Integer id ;
90
92
@ Column (name = "val" , nullable = false )
91
93
private String value ;
92
94
93
95
public Finish () {
94
96
}
95
97
96
- public Finish (String value ) {
98
+ public Finish (Integer id , String value ) {
99
+ this .id = id ;
97
100
this .value = value ;
98
101
}
99
102
100
- public long getId () {
103
+ public Integer getId () {
101
104
return id ;
102
105
}
103
106
104
- public void setId (long id ) {
105
- this .id = id ;
106
- }
107
-
108
107
public String getValue () {
109
108
return value ;
110
109
}
@@ -119,29 +118,23 @@ public void setValue(String value) {
119
118
@ FetchProfile .FetchOverride (entity = Mid .class , association = "finish" )
120
119
})
121
120
public static class Mid {
122
-
123
121
@ Id
124
- @ GeneratedValue
125
- private long id ;
126
-
122
+ private Integer id ;
127
123
@ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
128
124
private Finish finish ;
129
125
130
126
public Mid () {
131
127
}
132
128
133
- public Mid (Finish finish ) {
129
+ public Mid (Integer id , Finish finish ) {
130
+ this .id = id ;
134
131
this .finish = finish ;
135
132
}
136
133
137
- public long getId () {
134
+ public Integer getId () {
138
135
return id ;
139
136
}
140
137
141
- public void setId (long id ) {
142
- this .id = id ;
143
- }
144
-
145
138
public Finish getFinish () {
146
139
return finish ;
147
140
}
@@ -158,33 +151,26 @@ public void setFinish(Finish finish) {
158
151
@ FetchProfile .FetchOverride (entity = Start .class , association = "via2" )
159
152
})
160
153
public static class Start {
161
-
162
154
@ Id
163
- @ GeneratedValue
164
- private long id ;
165
-
155
+ private Integer id ;
166
156
@ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
167
157
private Via1 via1 ;
168
-
169
158
@ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
170
159
private Via2 via2 ;
171
160
172
161
public Start () {
173
162
}
174
163
175
- public Start (Via1 via1 , Via2 via2 ) {
164
+ public Start (Integer id , Via1 via1 , Via2 via2 ) {
165
+ this .id = id ;
176
166
this .via1 = via1 ;
177
167
this .via2 = via2 ;
178
168
}
179
169
180
- public long getId () {
170
+ public Integer getId () {
181
171
return id ;
182
172
}
183
173
184
- public void setId (long id ) {
185
- this .id = id ;
186
- }
187
-
188
174
public Via1 getVia1 () {
189
175
return via1 ;
190
176
}
@@ -208,29 +194,23 @@ public void setVia2(Via2 via2) {
208
194
@ FetchProfile .FetchOverride (entity = Via1 .class , association = "mid" )
209
195
})
210
196
public static class Via1 {
211
-
212
197
@ Id
213
- @ GeneratedValue
214
- private long id ;
215
-
198
+ private Integer id ;
216
199
@ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
217
200
private Mid mid ;
218
201
219
202
public Via1 () {
220
203
}
221
204
222
- public Via1 (Mid mid ) {
205
+ public Via1 (Integer id , Mid mid ) {
206
+ this .id = id ;
223
207
this .mid = mid ;
224
208
}
225
209
226
- public long getId () {
210
+ public Integer getId () {
227
211
return id ;
228
212
}
229
213
230
- public void setId (long id ) {
231
- this .id = id ;
232
- }
233
-
234
214
public Mid getMid () {
235
215
return mid ;
236
216
}
@@ -246,29 +226,23 @@ public void setMid(Mid mid) {
246
226
@ FetchProfile .FetchOverride (entity = Via2 .class , association = "mid" )
247
227
})
248
228
public static class Via2 {
249
-
250
229
@ Id
251
- @ GeneratedValue
252
- private long id ;
253
-
230
+ private Integer id ;
254
231
@ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
255
232
private Mid mid ;
256
233
257
234
public Via2 () {
258
235
}
259
236
260
- public Via2 (Mid mid ) {
237
+ public Via2 (Integer id , Mid mid ) {
238
+ this .id = id ;
261
239
this .mid = mid ;
262
240
}
263
241
264
- public long getId () {
242
+ public Integer getId () {
265
243
return id ;
266
244
}
267
245
268
- public void setId (long id ) {
269
- this .id = id ;
270
- }
271
-
272
246
public Mid getMid () {
273
247
return mid ;
274
248
}
0 commit comments