1010import jakarta .persistence .FetchType ;
1111import jakarta .persistence .Id ;
1212import jakarta .persistence .ManyToOne ;
13+ import jakarta .persistence .Table ;
1314import org .hibernate .LazyInitializationException ;
1415import org .hibernate .annotations .FetchProfile ;
1516import org .hibernate .stat .spi .StatisticsImplementor ;
3334 settings = @ Setting ( name = GENERATE_STATISTICS , value = "true" )
3435)
3536@ DomainModel ( annotatedClasses = {
36- EntityLoadedInTwoPhaseLoadTest .Start .class ,
37- EntityLoadedInTwoPhaseLoadTest .Mid .class ,
38- EntityLoadedInTwoPhaseLoadTest .Finish .class ,
39- EntityLoadedInTwoPhaseLoadTest .Via1 .class ,
40- EntityLoadedInTwoPhaseLoadTest .Via2 .class
37+ EntityLoadedInTwoPhaseLoadTest .StartNode .class ,
38+ EntityLoadedInTwoPhaseLoadTest .MiddleNode .class ,
39+ EntityLoadedInTwoPhaseLoadTest .TerminalNode .class ,
40+ EntityLoadedInTwoPhaseLoadTest .Branch1Node .class ,
41+ EntityLoadedInTwoPhaseLoadTest .Branch2Node .class
4142} )
4243@ SessionFactory
4344public class EntityLoadedInTwoPhaseLoadTest {
@@ -48,9 +49,9 @@ public void testIfAllRelationsAreInitialized(SessionFactoryScope sessions) {
4849 final StatisticsImplementor statistics = sessions .getSessionFactory ().getStatistics ();
4950 statistics .clear ();
5051
51- final Start start = sessions .fromTransaction ( (session ) -> {
52+ final StartNode startNode = sessions .fromTransaction ( (session ) -> {
5253 session .enableFetchProfile ( FETCH_PROFILE_NAME );
53- return session .find ( Start .class , 1 );
54+ return session .find ( StartNode .class , 1 );
5455 } );
5556
5657 // should have loaded all the data
@@ -60,8 +61,7 @@ public void testIfAllRelationsAreInitialized(SessionFactoryScope sessions) {
6061
6162 try {
6263 // access the data which was supposed to have been fetched
63- //noinspection ResultOfMethodCallIgnored
64- start .getVia2 ().getMid ().getFinish ().getValue ();
64+ assertThat ( startNode .branch2Node .middleNode .terminalNode .name ).isNotNull ();
6565 }
6666 catch (LazyInitializationException e ) {
6767 fail ( "Everything should be initialized" );
@@ -71,12 +71,12 @@ public void testIfAllRelationsAreInitialized(SessionFactoryScope sessions) {
7171 @ BeforeEach
7272 void createTestData (SessionFactoryScope sessions ) {
7373 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 );
74+ TerminalNode terminalNode = new TerminalNode ( 1 , "foo" );
75+ MiddleNode middleNode = new MiddleNode ( 1 , terminalNode );
76+ Branch2Node branch2Node = new Branch2Node ( 1 , middleNode );
77+ StartNode startNode = new StartNode ( 1 , null , branch2Node );
7878
79- session .persist ( start );
79+ session .persist ( startNode );
8080 } );
8181 }
8282
@@ -85,171 +85,107 @@ void dropTestData(SessionFactoryScope sessions) {
8585 sessions .dropData ();
8686 }
8787
88- @ Entity (name = "FinishEntity" )
89- public static class Finish {
88+ @ Entity
89+ @ Table (name ="start_node" )
90+ @ FetchProfile (name = FETCH_PROFILE_NAME , fetchOverrides = {
91+ @ FetchProfile .FetchOverride (entity = StartNode .class , association = "branch1Node" ),
92+ @ FetchProfile .FetchOverride (entity = StartNode .class , association = "branch2Node" )
93+ })
94+ @ SuppressWarnings ({"FieldCanBeLocal" , "unused" })
95+ public static class StartNode {
9096 @ Id
9197 private Integer id ;
92- @ Column (name = "val" , nullable = false )
93- private String value ;
98+ @ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
99+ private Branch1Node branch1Node ;
100+ @ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
101+ private Branch2Node branch2Node ;
94102
95- public Finish () {
103+ public StartNode () {
96104 }
97105
98- public Finish (Integer id , String value ) {
106+ public StartNode (Integer id , Branch1Node branch1Node , Branch2Node branch2Node ) {
99107 this .id = id ;
100- this .value = value ;
101- }
102-
103- public Integer getId () {
104- return id ;
105- }
106-
107- public String getValue () {
108- return value ;
109- }
110-
111- public void setValue (String value ) {
112- this .value = value ;
108+ this .branch1Node = branch1Node ;
109+ this .branch2Node = branch2Node ;
113110 }
114111 }
115112
116- @ Entity (name = "MidEntity " )
113+ @ Entity (name = "Via1Entity " )
117114 @ FetchProfile (name = FETCH_PROFILE_NAME , fetchOverrides = {
118- @ FetchProfile .FetchOverride (entity = Mid .class , association = "finish " )
115+ @ FetchProfile .FetchOverride (entity = Branch1Node .class , association = "middleNode " )
119116 })
120- public static class Mid {
117+ @ SuppressWarnings ({"FieldCanBeLocal" , "unused" })
118+ public static class Branch1Node {
121119 @ Id
122120 private Integer id ;
123121 @ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
124- private Finish finish ;
122+ private MiddleNode middleNode ;
125123
126- public Mid () {
124+ public Branch1Node () {
127125 }
128126
129- public Mid (Integer id , Finish finish ) {
127+ public Branch1Node (Integer id , MiddleNode middleNode ) {
130128 this .id = id ;
131- this .finish = finish ;
132- }
133-
134- public Integer getId () {
135- return id ;
129+ this .middleNode = middleNode ;
136130 }
137-
138- public Finish getFinish () {
139- return finish ;
140- }
141-
142- public void setFinish (Finish finish ) {
143- this .finish = finish ;
144- }
145-
146131 }
147132
148- @ Entity (name = "StartEntity " )
133+ @ Entity (name = "Via2Entity " )
149134 @ FetchProfile (name = FETCH_PROFILE_NAME , fetchOverrides = {
150- @ FetchProfile .FetchOverride (entity = Start .class , association = "via1" ),
151- @ FetchProfile .FetchOverride (entity = Start .class , association = "via2" )
135+ @ FetchProfile .FetchOverride (entity = Branch2Node .class , association = "middleNode" )
152136 })
153- public static class Start {
137+ @ SuppressWarnings ({"FieldCanBeLocal" , "unused" })
138+ public static class Branch2Node {
154139 @ Id
155140 private Integer id ;
156141 @ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
157- private Via1 via1 ;
158- @ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
159- private Via2 via2 ;
142+ private MiddleNode middleNode ;
160143
161- public Start () {
144+ public Branch2Node () {
162145 }
163146
164- public Start (Integer id , Via1 via1 , Via2 via2 ) {
147+ public Branch2Node (Integer id , MiddleNode middleNode ) {
165148 this .id = id ;
166- this .via1 = via1 ;
167- this .via2 = via2 ;
168- }
169-
170- public Integer getId () {
171- return id ;
172- }
173-
174- public Via1 getVia1 () {
175- return via1 ;
176- }
177-
178- public void setVia1 (Via1 via1 ) {
179- this .via1 = via1 ;
180- }
181-
182- public Via2 getVia2 () {
183- return via2 ;
149+ this .middleNode = middleNode ;
184150 }
185-
186- public void setVia2 (Via2 via2 ) {
187- this .via2 = via2 ;
188- }
189-
190151 }
191152
192- @ Entity (name = "Via1Entity" )
153+ @ Entity
154+ @ Table (name ="middle_node" )
193155 @ FetchProfile (name = FETCH_PROFILE_NAME , fetchOverrides = {
194- @ FetchProfile .FetchOverride (entity = Via1 .class , association = "mid " )
156+ @ FetchProfile .FetchOverride (entity = MiddleNode .class , association = "terminalNode " )
195157 })
196- public static class Via1 {
158+ @ SuppressWarnings ({"FieldCanBeLocal" , "unused" })
159+ public static class MiddleNode {
197160 @ Id
198161 private Integer id ;
199162 @ ManyToOne (fetch = FetchType .LAZY , cascade = CascadeType .PERSIST )
200- private Mid mid ;
163+ private TerminalNode terminalNode ;
201164
202- public Via1 () {
165+ public MiddleNode () {
203166 }
204167
205- public Via1 (Integer id , Mid mid ) {
168+ public MiddleNode (Integer id , TerminalNode terminalNode ) {
206169 this .id = id ;
207- this .mid = mid ;
208- }
209-
210- public Integer getId () {
211- return id ;
212- }
213-
214- public Mid getMid () {
215- return mid ;
216- }
217-
218- public void setMid (Mid mid ) {
219- this .mid = mid ;
170+ this .terminalNode = terminalNode ;
220171 }
221-
222172 }
223173
224- @ Entity (name = "Via2Entity" )
225- @ FetchProfile (name = FETCH_PROFILE_NAME , fetchOverrides = {
226- @ FetchProfile .FetchOverride (entity = Via2 .class , association = "mid" )
227- })
228- public static class Via2 {
174+ @ Entity
175+ @ Table (name ="terminal_node" )
176+ @ SuppressWarnings ({"FieldCanBeLocal" , "unused" })
177+ public static class TerminalNode {
229178 @ Id
230179 private Integer id ;
231- @ ManyToOne ( fetch = FetchType . LAZY , cascade = CascadeType . PERSIST )
232- private Mid mid ;
180+ @ Column ( nullable = false )
181+ private String name ;
233182
234- public Via2 () {
183+ public TerminalNode () {
235184 }
236185
237- public Via2 (Integer id , Mid mid ) {
186+ public TerminalNode (Integer id , String name ) {
238187 this .id = id ;
239- this .mid = mid ;
240- }
241-
242- public Integer getId () {
243- return id ;
244- }
245-
246- public Mid getMid () {
247- return mid ;
188+ this .name = name ;
248189 }
249-
250- public void setMid (Mid mid ) {
251- this .mid = mid ;
252- }
253-
254190 }
255191}
0 commit comments