88
99import jakarta .persistence .EntityManager ;
1010
11- import org .junit .Test ;
11+ import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
12+ import org .hibernate .testing .orm .junit .Jpa ;
13+ import org .junit .jupiter .api .AfterEach ;
1214
13- import org .hibernate .orm .test .jpa .BaseEntityManagerFunctionalTestCase ;
1415import org .hibernate .testing .orm .junit .JiraKey ;
16+ import org .junit .jupiter .api .BeforeEach ;
17+ import org .junit .jupiter .api .Test ;
1518
16- import static org .junit .Assert .assertEquals ;
17- import static org .junit .Assert .assertNotNull ;
18- import static org .junit .Assert .assertNull ;
19+ import static org .junit .jupiter . api . Assertions .assertEquals ;
20+ import static org .junit .jupiter . api . Assertions .assertNotNull ;
21+ import static org .junit .jupiter . api . Assertions .assertNull ;
1922
2023/**
2124 * @author Steve Ebersole
2225 * @author Gail Badner
2326 */
24- public class DeleteMultiLevelOrphansTest extends BaseEntityManagerFunctionalTestCase {
25-
26- private void createData () {
27- Preisregelung preisregelung = new Preisregelung ();
28-
29- Tranchenmodell tranchenmodell = new Tranchenmodell ();
30-
31- X x = new X ();
32-
33- Tranche tranche1 = new Tranche ();
34-
35- Y y = new Y ();
36-
37- Tranche tranche2 = new Tranche ();
38-
39- preisregelung .setTranchenmodell ( tranchenmodell );
40- tranchenmodell .setPreisregelung ( preisregelung );
41-
42- tranchenmodell .setX ( x );
43- x .setTranchenmodell ( tranchenmodell );
44-
45- tranchenmodell .getTranchen ().add ( tranche1 );
46- tranche1 .setTranchenmodell ( tranchenmodell );
47- tranchenmodell .getTranchen ().add ( tranche2 );
48- tranche2 .setTranchenmodell ( tranchenmodell );
49-
50- tranche1 .setY ( y );
51- y .setTranche ( tranche1 );
52-
53- EntityManager em = getOrCreateEntityManager ();
54- em .getTransaction ().begin ();
55- em .persist ( preisregelung );
56- em .getTransaction ().commit ();
57- em .close ();
27+ @ Jpa (
28+ annotatedClasses = {
29+ Preisregelung .class ,
30+ Tranche .class ,
31+ Tranchenmodell .class ,
32+ X .class ,
33+ Y .class
34+ }
35+ )
36+ public class DeleteMultiLevelOrphansTest {
37+
38+ @ BeforeEach
39+ public void createData (EntityManagerFactoryScope scope ) {
40+ scope .inTransaction ( entityManager -> {
41+ Preisregelung preisregelung = new Preisregelung ();
42+ Tranchenmodell tranchenmodell = new Tranchenmodell ();
43+ X x = new X ();
44+ Tranche tranche1 = new Tranche ();
45+ Y y = new Y ();
46+ Tranche tranche2 = new Tranche ();
47+
48+ preisregelung .setTranchenmodell ( tranchenmodell );
49+ tranchenmodell .setPreisregelung ( preisregelung );
50+
51+ tranchenmodell .setX ( x );
52+ x .setTranchenmodell ( tranchenmodell );
53+
54+ tranchenmodell .getTranchen ().add ( tranche1 );
55+ tranche1 .setTranchenmodell ( tranchenmodell );
56+ tranchenmodell .getTranchen ().add ( tranche2 );
57+ tranche2 .setTranchenmodell ( tranchenmodell );
58+
59+ tranche1 .setY ( y );
60+ y .setTranche ( tranche1 );
61+
62+ entityManager .persist ( preisregelung );
63+ } );
5864 }
5965
60- private void cleanupData () {
61- EntityManager em = getOrCreateEntityManager ();
62- em .getTransaction ().begin ();
63- em .createQuery ( "delete Tranche" ).executeUpdate ();
64- em .createQuery ( "delete Tranchenmodell" ).executeUpdate ();
65- em .createQuery ( "delete Preisregelung" ).executeUpdate ();
66- em .getTransaction ().commit ();
67- em .close ();
66+ @ AfterEach
67+ public void cleanupData (EntityManagerFactoryScope scope ) {
68+ scope .inTransaction ( entityManager -> scope .getEntityManagerFactory ().getSchemaManager ().truncate () );
6869 }
6970
7071 @ Test
7172 @ JiraKey ( value = "HHH-9091" )
72- public void testDirectAssociationOrphanedWhileManaged () {
73- createData ();
74-
75- EntityManager em = getOrCreateEntityManager ();
76- em .getTransaction ().begin ();
77- List results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
78- assertEquals ( 1 , results .size () );
79- results = em .createQuery ( "from Preisregelung" ).getResultList ();
80- assertEquals ( 1 , results .size () );
81- Preisregelung preisregelung = (Preisregelung ) results .get ( 0 );
82- Tranchenmodell tranchenmodell = preisregelung .getTranchenmodell ();
83- assertNotNull ( tranchenmodell );
84- assertNotNull ( tranchenmodell .getX () );
85- assertEquals ( 2 , tranchenmodell .getTranchen ().size () );
86- assertNotNull ( tranchenmodell .getTranchen ().get ( 0 ).getY () );
87- preisregelung .setTranchenmodell ( null );
88- em .getTransaction ().commit ();
89- em .close ();
90-
91- em = getOrCreateEntityManager ();
92- em .getTransaction ().begin ();
93-
94- preisregelung = (Preisregelung ) em .find ( Preisregelung .class , preisregelung .getId () );
95- assertNull ( preisregelung .getTranchenmodell () );
96- results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
97- assertEquals ( 0 , results .size () );
98- results = em .createQuery ( "from Tranche" ).getResultList ();
99- assertEquals ( 0 , results .size () );
100- results = em .createQuery ( "from X" ).getResultList ();
101- assertEquals ( 0 , results .size () );
102- results = em .createQuery ( "from Y" ).getResultList ();
103- assertEquals ( 0 , results .size () );
104-
105- results = em .createQuery ( "from Preisregelung" ).getResultList ();
106- assertEquals ( 1 , results .size () );
107-
108- em .getTransaction ().commit ();
109- em .close ();
110-
111- cleanupData ();
73+ public void testDirectAssociationOrphanedWhileManaged (EntityManagerFactoryScope scope ) {
74+ Long id = scope .fromTransaction (
75+ em -> {
76+ List results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
77+ assertEquals ( 1 , results .size () );
78+ results = em .createQuery ( "from Preisregelung" ).getResultList ();
79+ assertEquals ( 1 , results .size () );
80+ Preisregelung preisregelung = (Preisregelung ) results .get ( 0 );
81+ Tranchenmodell tranchenmodell = preisregelung .getTranchenmodell ();
82+ assertNotNull ( tranchenmodell );
83+ assertNotNull ( tranchenmodell .getX () );
84+ assertEquals ( 2 , tranchenmodell .getTranchen ().size () );
85+ assertNotNull ( tranchenmodell .getTranchen ().get ( 0 ).getY () );
86+ preisregelung .setTranchenmodell ( null );
87+
88+ return preisregelung .getId ();
89+ }
90+ );
91+
92+ scope .inTransaction (
93+ em -> {
94+ Preisregelung preisregelung = em .find ( Preisregelung .class , id );
95+ assertNull ( preisregelung .getTranchenmodell () );
96+ List results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
97+ assertEquals ( 0 , results .size () );
98+ results = em .createQuery ( "from Tranche" ).getResultList ();
99+ assertEquals ( 0 , results .size () );
100+ results = em .createQuery ( "from X" ).getResultList ();
101+ assertEquals ( 0 , results .size () );
102+ results = em .createQuery ( "from Y" ).getResultList ();
103+ assertEquals ( 0 , results .size () );
104+
105+ results = em .createQuery ( "from Preisregelung" ).getResultList ();
106+ assertEquals ( 1 , results .size () );
107+ }
108+ );
112109 }
113110
114111 @ Test
115112 @ JiraKey ( value = "HHH-9091" )
116- public void testReplacedDirectAssociationWhileManaged () {
117- createData ();
118-
119- EntityManager em = getOrCreateEntityManager ();
113+ public void testReplacedDirectAssociationWhileManaged (EntityManagerFactoryScope scope ) {
114+ EntityManager em = scope .getEntityManagerFactory ().createEntityManager ();
120115 em .getTransaction ().begin ();
116+
121117 List results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
122118 assertEquals ( 1 , results .size () );
123119 results = em .createQuery ( "from Preisregelung" ).getResultList ();
@@ -148,7 +144,7 @@ public void testReplacedDirectAssociationWhileManaged() {
148144 em .getTransaction ().commit ();
149145 em .close ();
150146
151- em = getOrCreateEntityManager ();
147+ em = scope . getEntityManagerFactory (). createEntityManager ();
152148 em .getTransaction ().begin ();
153149
154150 results = em .createQuery ( "from Tranche" ).getResultList ();
@@ -175,11 +171,13 @@ public void testReplacedDirectAssociationWhileManaged() {
175171 tranchenmodellNew = new Tranchenmodell ();
176172 preisregelung .setTranchenmodell (tranchenmodellNew );
177173 tranchenmodellNew .setPreisregelung ( preisregelung );
174+
178175 em .getTransaction ().commit ();
179176 em .close ();
180177
181- em = getOrCreateEntityManager ();
178+ em = scope . getEntityManagerFactory (). createEntityManager ();
182179 em .getTransaction ().begin ();
180+
183181 results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
184182 assertEquals ( 1 , results .size () );
185183 tranchenmodell = (Tranchenmodell ) results .get ( 0 );
@@ -194,67 +192,50 @@ public void testReplacedDirectAssociationWhileManaged() {
194192 assertEquals ( 0 , results .size () );
195193 results = em .createQuery ( "from Y" ).getResultList ();
196194 assertEquals ( 0 , results .size () );
195+
197196 em .getTransaction ().commit ();
198197 em .close ();
199-
200- cleanupData ();
201198 }
202199
203200 @ Test
204201 @ JiraKey ( value = "HHH-9091" )
205- public void testDirectAndNestedAssociationsOrphanedWhileManaged () {
206- createData ();
207-
208- EntityManager em = getOrCreateEntityManager ();
209- em .getTransaction ().begin ();
210- List results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
211- assertEquals ( 1 , results .size () );
212- results = em .createQuery ( "from Preisregelung" ).getResultList ();
213- assertEquals ( 1 , results .size () );
214- Preisregelung preisregelung = (Preisregelung ) results .get ( 0 );
215- Tranchenmodell tranchenmodell = preisregelung .getTranchenmodell ();
216- assertNotNull ( tranchenmodell );
217- assertNotNull ( tranchenmodell .getX () );
218- assertEquals ( 2 , tranchenmodell .getTranchen ().size () );
219- assertNotNull ( tranchenmodell .getTranchen ().get ( 0 ).getY () );
220- preisregelung .setTranchenmodell ( null );
221- tranchenmodell .setX ( null );
222- tranchenmodell .getTranchen ().get ( 0 ).setY ( null );
223- em .getTransaction ().commit ();
224- em .close ();
225-
226- em = getOrCreateEntityManager ();
227- em .getTransaction ().begin ();
228-
229- preisregelung = (Preisregelung ) em .find ( Preisregelung .class , preisregelung .getId () );
230- assertNull ( preisregelung .getTranchenmodell () );
231- results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
232- assertEquals ( 0 , results .size () );
233- results = em .createQuery ( "from Tranche" ).getResultList ();
234- assertEquals ( 0 , results .size () );
235- results = em .createQuery ( "from X" ).getResultList ();
236- assertEquals ( 0 , results .size () );
237- results = em .createQuery ( "from Y" ).getResultList ();
238- assertEquals ( 0 , results .size () );
239-
240- results = em .createQuery ( "from Preisregelung" ).getResultList ();
241- assertEquals ( 1 , results .size () );
242-
243- em .getTransaction ().commit ();
244- em .close ();
245-
246- cleanupData ();
247- }
248-
249- @ Override
250- protected Class <?>[] getAnnotatedClasses () {
251- return new Class []{
252- Preisregelung .class ,
253- Tranche .class ,
254- Tranchenmodell .class ,
255- X .class ,
256- Y .class
257- };
202+ public void testDirectAndNestedAssociationsOrphanedWhileManaged (EntityManagerFactoryScope scope ) {
203+ Long id = scope .fromTransaction (
204+ em -> {
205+ List results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
206+ assertEquals ( 1 , results .size () );
207+ results = em .createQuery ( "from Preisregelung" ).getResultList ();
208+ assertEquals ( 1 , results .size () );
209+ Preisregelung preisregelung = (Preisregelung ) results .get ( 0 );
210+ Tranchenmodell tranchenmodell = preisregelung .getTranchenmodell ();
211+ assertNotNull ( tranchenmodell );
212+ assertNotNull ( tranchenmodell .getX () );
213+ assertEquals ( 2 , tranchenmodell .getTranchen ().size () );
214+ assertNotNull ( tranchenmodell .getTranchen ().get ( 0 ).getY () );
215+ preisregelung .setTranchenmodell ( null );
216+ tranchenmodell .setX ( null );
217+ tranchenmodell .getTranchen ().get ( 0 ).setY ( null );
218+
219+ return preisregelung .getId ();
220+ }
221+ );
222+ scope .inTransaction (
223+ em -> {
224+ Preisregelung preisregelung = em .find ( Preisregelung .class , id );
225+ assertNull ( preisregelung .getTranchenmodell () );
226+ List results = em .createQuery ( "from Tranchenmodell" ).getResultList ();
227+ assertEquals ( 0 , results .size () );
228+ results = em .createQuery ( "from Tranche" ).getResultList ();
229+ assertEquals ( 0 , results .size () );
230+ results = em .createQuery ( "from X" ).getResultList ();
231+ assertEquals ( 0 , results .size () );
232+ results = em .createQuery ( "from Y" ).getResultList ();
233+ assertEquals ( 0 , results .size () );
234+
235+ results = em .createQuery ( "from Preisregelung" ).getResultList ();
236+ assertEquals ( 1 , results .size () );
237+ }
238+ );
258239 }
259240
260241}
0 commit comments