Skip to content

Commit cb9853d

Browse files
committed
Various code cleanup
1 parent 8924308 commit cb9853d

File tree

1 file changed

+74
-100
lines changed

1 file changed

+74
-100
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/fetchprofiles/EntityLoadedInTwoPhaseLoadTest.java

Lines changed: 74 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -8,103 +8,102 @@
88
import jakarta.persistence.Column;
99
import jakarta.persistence.Entity;
1010
import jakarta.persistence.FetchType;
11-
import jakarta.persistence.GeneratedValue;
1211
import jakarta.persistence.Id;
1312
import jakarta.persistence.ManyToOne;
14-
1513
import org.hibernate.LazyInitializationException;
1614
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;
2017
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")
2831
@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 {
3144
static final String FETCH_PROFILE_NAME = "fp1";
3245

33-
public void configure(Configuration cfg) {
34-
cfg.setProperty( Environment.GENERATE_STATISTICS, true );
35-
}
36-
3746
@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+
4161
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();
4665
}
4766
catch (LazyInitializationException e) {
4867
fail( "Everything should be initialized" );
4968
}
5069
}
5170

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 );
6578

6679
session.persist( start );
67-
68-
return start.getId();
6980
} );
7081
}
7182

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();
8186
}
8287

8388
@Entity(name = "FinishEntity")
8489
public static class Finish {
85-
8690
@Id
87-
@GeneratedValue
88-
private long id;
89-
91+
private Integer id;
9092
@Column(name = "val", nullable = false)
9193
private String value;
9294

9395
public Finish() {
9496
}
9597

96-
public Finish(String value) {
98+
public Finish(Integer id, String value) {
99+
this.id = id;
97100
this.value = value;
98101
}
99102

100-
public long getId() {
103+
public Integer getId() {
101104
return id;
102105
}
103106

104-
public void setId(long id) {
105-
this.id = id;
106-
}
107-
108107
public String getValue() {
109108
return value;
110109
}
@@ -119,29 +118,23 @@ public void setValue(String value) {
119118
@FetchProfile.FetchOverride(entity = Mid.class, association = "finish")
120119
})
121120
public static class Mid {
122-
123121
@Id
124-
@GeneratedValue
125-
private long id;
126-
122+
private Integer id;
127123
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
128124
private Finish finish;
129125

130126
public Mid() {
131127
}
132128

133-
public Mid(Finish finish) {
129+
public Mid(Integer id, Finish finish) {
130+
this.id = id;
134131
this.finish = finish;
135132
}
136133

137-
public long getId() {
134+
public Integer getId() {
138135
return id;
139136
}
140137

141-
public void setId(long id) {
142-
this.id = id;
143-
}
144-
145138
public Finish getFinish() {
146139
return finish;
147140
}
@@ -158,33 +151,26 @@ public void setFinish(Finish finish) {
158151
@FetchProfile.FetchOverride(entity = Start.class, association = "via2")
159152
})
160153
public static class Start {
161-
162154
@Id
163-
@GeneratedValue
164-
private long id;
165-
155+
private Integer id;
166156
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
167157
private Via1 via1;
168-
169158
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
170159
private Via2 via2;
171160

172161
public Start() {
173162
}
174163

175-
public Start(Via1 via1, Via2 via2) {
164+
public Start(Integer id, Via1 via1, Via2 via2) {
165+
this.id = id;
176166
this.via1 = via1;
177167
this.via2 = via2;
178168
}
179169

180-
public long getId() {
170+
public Integer getId() {
181171
return id;
182172
}
183173

184-
public void setId(long id) {
185-
this.id = id;
186-
}
187-
188174
public Via1 getVia1() {
189175
return via1;
190176
}
@@ -208,29 +194,23 @@ public void setVia2(Via2 via2) {
208194
@FetchProfile.FetchOverride(entity = Via1.class, association = "mid")
209195
})
210196
public static class Via1 {
211-
212197
@Id
213-
@GeneratedValue
214-
private long id;
215-
198+
private Integer id;
216199
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
217200
private Mid mid;
218201

219202
public Via1() {
220203
}
221204

222-
public Via1(Mid mid) {
205+
public Via1(Integer id, Mid mid) {
206+
this.id = id;
223207
this.mid = mid;
224208
}
225209

226-
public long getId() {
210+
public Integer getId() {
227211
return id;
228212
}
229213

230-
public void setId(long id) {
231-
this.id = id;
232-
}
233-
234214
public Mid getMid() {
235215
return mid;
236216
}
@@ -246,29 +226,23 @@ public void setMid(Mid mid) {
246226
@FetchProfile.FetchOverride(entity = Via2.class, association = "mid")
247227
})
248228
public static class Via2 {
249-
250229
@Id
251-
@GeneratedValue
252-
private long id;
253-
230+
private Integer id;
254231
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
255232
private Mid mid;
256233

257234
public Via2() {
258235
}
259236

260-
public Via2(Mid mid) {
237+
public Via2(Integer id, Mid mid) {
238+
this.id = id;
261239
this.mid = mid;
262240
}
263241

264-
public long getId() {
242+
public Integer getId() {
265243
return id;
266244
}
267245

268-
public void setId(long id) {
269-
this.id = id;
270-
}
271-
272246
public Mid getMid() {
273247
return mid;
274248
}

0 commit comments

Comments
 (0)