|
5 | 5 | package org.hibernate.orm.test.unidir; |
6 | 6 |
|
7 | 7 | import org.hibernate.Hibernate; |
8 | | -import org.hibernate.Session; |
9 | | -import org.hibernate.Transaction; |
10 | | -import org.hibernate.cfg.Configuration; |
11 | | -import org.hibernate.metamodel.CollectionClassification; |
12 | | - |
13 | | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
14 | | -import org.junit.Test; |
15 | | - |
16 | | -import static org.hibernate.cfg.AvailableSettings.DEFAULT_LIST_SEMANTICS; |
17 | | -import static org.junit.Assert.assertEquals; |
18 | | -import static org.junit.Assert.assertFalse; |
| 8 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.junit.jupiter.api.Test; |
19 | 14 |
|
20 | 15 | /** |
21 | 16 | * @author Gavin King |
22 | 17 | */ |
23 | | -public class BackrefTest extends BaseCoreFunctionalTestCase { |
24 | | - |
25 | | - @Override |
26 | | - protected String getBaseForMappings() { |
27 | | - return "org/hibernate/orm/test/"; |
28 | | - } |
29 | | - |
30 | | - @Override |
31 | | - protected String[] getMappings() { |
32 | | - return new String[] { "unidir/ParentChild.hbm.xml" }; |
33 | | - } |
34 | | - |
35 | | - @Override |
36 | | - protected Class<?>[] getAnnotatedClasses() { |
37 | | - // No test needed at this time. This was purely to test a |
38 | | - // validation issue from HHH-5836. |
39 | | - return new Class<?>[] { Parent1.class, Child1.class, Child2.class }; |
40 | | - } |
41 | | - |
42 | | - @Override |
43 | | - protected void configure(Configuration configuration) { |
44 | | - super.configure( configuration ); |
45 | | - configuration.setProperty( DEFAULT_LIST_SEMANTICS, CollectionClassification.BAG ); |
| 18 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 19 | +@DomainModel( |
| 20 | + xmlMappings = "org/hibernate/orm/test/unidir/ParentChild.hbm.xml" |
| 21 | +// annotatedClasses = { Parent1.class, Child1.class, Child2.class } |
| 22 | +) |
| 23 | +@SessionFactory |
| 24 | +public class BackrefTest { |
| 25 | + @AfterEach |
| 26 | + void tearDown(SessionFactoryScope factoryScope) { |
| 27 | + factoryScope.dropData(); |
46 | 28 | } |
47 | 29 |
|
48 | 30 | @Test |
49 | | - public void testBackRef() { |
50 | | - Session s = openSession(); |
51 | | - Transaction t = s.beginTransaction(); |
52 | | - Parent p = new Parent("Marc", 123456789); |
53 | | - Parent p2 = new Parent("Nathalie", 987654321 ); |
54 | | - Child c = new Child("Elvira"); |
55 | | - Child c2 = new Child("Blase"); |
56 | | - p.getChildren().add(c); |
57 | | - p.getChildren().add(c2); |
58 | | - s.persist(p); |
59 | | - s.persist(p2); |
60 | | - t.commit(); |
61 | | - s.close(); |
62 | | - |
63 | | - s = openSession(); |
64 | | - t = s.beginTransaction(); |
65 | | - c = (Child) s.get(Child.class, "Elvira"); |
66 | | - c.setAge(2); |
67 | | - t.commit(); |
68 | | - s.close(); |
69 | | - |
70 | | - s = openSession(); |
71 | | - t = s.beginTransaction(); |
72 | | - p = (Parent) s.get(Parent.class, "Marc"); |
73 | | - c = (Child) s.get(Child.class, "Elvira"); |
74 | | - c.setAge(18); |
75 | | - t.commit(); |
76 | | - s.close(); |
77 | | - |
78 | | - s = openSession(); |
79 | | - t = s.beginTransaction(); |
80 | | - p = (Parent) s.get(Parent.class, "Marc"); |
81 | | - p2 = (Parent) s.get(Parent.class, "Nathalie"); |
82 | | - c = (Child) s.get(Child.class, "Elvira"); |
83 | | - assertEquals( p.getChildren().indexOf(c), 0 ); |
84 | | - p.getChildren().remove(c); |
85 | | - p2.getChildren().add(c); |
86 | | - t.commit(); |
87 | | - |
88 | | - s.close(); |
89 | | - s = openSession(); |
90 | | - t = s.beginTransaction(); |
91 | | - Parent p3 = new Parent("Marion", 543216789); |
92 | | - p3.getChildren().add( new Child("Gavin") ); |
93 | | - s.merge(p3); |
94 | | - t.commit(); |
95 | | - s.close(); |
96 | | - |
97 | | - s = openSession(); |
98 | | - t = s.beginTransaction(); |
99 | | - s.createQuery( "delete from Child" ).executeUpdate(); |
100 | | - s.createQuery( "delete from Parent" ).executeUpdate(); |
101 | | - t.commit(); |
102 | | - s.close(); |
| 31 | + public void testBackRef(SessionFactoryScope factoryScope) { |
| 32 | + factoryScope.inTransaction( (session) -> { |
| 33 | + Parent p = new Parent("Marc", 123456789); |
| 34 | + Parent p2 = new Parent("Nathalie", 987654321 ); |
| 35 | + Child c = new Child("Elvira"); |
| 36 | + Child c2 = new Child("Blase"); |
| 37 | + p.getChildren().add(c); |
| 38 | + p.getChildren().add(c2); |
| 39 | + session.persist(p); |
| 40 | + session.persist(p2); |
| 41 | + } ); |
| 42 | + |
| 43 | + factoryScope.inTransaction( (s) -> { |
| 44 | + var c = s.find( Child.class, "Elvira" ); |
| 45 | + c.setAge(2); |
| 46 | + } ); |
| 47 | + |
| 48 | + factoryScope.inTransaction( (s) -> { |
| 49 | + var c = s.find( Child.class, "Elvira" ); |
| 50 | + c.setAge(18); |
| 51 | + } ); |
| 52 | + |
| 53 | + factoryScope.inTransaction( (s) -> { |
| 54 | + var p = s.find( Parent.class, "Marc" ); |
| 55 | + var p2 = s.find( Parent.class, "Nathalie" ); |
| 56 | + var c = s.find( Child.class, "Elvira" ); |
| 57 | + Assertions.assertEquals( 0, p.getChildren().indexOf(c) ); |
| 58 | + p.getChildren().remove(c); |
| 59 | + p2.getChildren().add(c); |
| 60 | + } ); |
| 61 | + |
| 62 | + factoryScope.inTransaction( (s) -> { |
| 63 | + var p3 = new Parent("Marion", 543216789); |
| 64 | + p3.getChildren().add( new Child("Gavin") ); |
| 65 | + s.merge(p3); |
| 66 | + } ); |
103 | 67 | } |
104 | 68 |
|
105 | 69 | @Test |
106 | | - public void testBackRefToProxiedEntityOnMerge() { |
107 | | - Session s = openSession(); |
108 | | - s.beginTransaction(); |
109 | | - Parent me = new Parent( "Steve", 192837465 ); |
110 | | - me.getChildren().add( new Child( "Joe" ) ); |
111 | | - s.persist( me ); |
112 | | - s.getTransaction().commit(); |
113 | | - s.close(); |
| 70 | + public void testBackRefToProxiedEntityOnMerge(SessionFactoryScope factoryScope) { |
| 71 | + var me = factoryScope.fromTransaction( (s) -> { |
| 72 | + var steve = new Parent( "Steve", 192837465 ); |
| 73 | + steve.getChildren().add( new Child( "Joe" ) ); |
| 74 | + s.persist( steve ); |
| 75 | + return steve; |
| 76 | + } ); |
114 | 77 |
|
115 | 78 | // while detached, add a new element |
116 | 79 | me.getChildren().add( new Child( "Cece" ) ); |
117 | 80 | me.getChildren().add( new Child( "Austin" ) ); |
118 | 81 |
|
119 | | - s = openSession(); |
120 | | - s.beginTransaction(); |
121 | 82 | // load 'me' to associate it with the new session as a proxy (this may have occurred as 'prior work' |
122 | 83 | // to the reattachment below)... |
123 | | - Object meProxy = s.getReference( Parent.class, me.getName() ); |
124 | | - assertFalse( Hibernate.isInitialized( meProxy ) ); |
125 | | - // now, do the reattchment... |
126 | | - s.merge( me ); |
127 | | - s.getTransaction().commit(); |
128 | | - s.close(); |
129 | | - |
130 | | - s = openSession(); |
131 | | - s.beginTransaction(); |
132 | | - s.createQuery( "delete from Child" ).executeUpdate(); |
133 | | - s.createQuery( "delete from Parent" ).executeUpdate(); |
134 | | - s.getTransaction().commit(); |
135 | | - s.close(); |
| 84 | + factoryScope.inTransaction( (s) -> { |
| 85 | + Object meProxy = s.getReference( Parent.class, me.getName() ); |
| 86 | + Assertions.assertFalse( Hibernate.isInitialized( meProxy ) ); |
| 87 | + // now, do the reattchment... |
| 88 | + s.merge( me ); |
| 89 | + } ); |
136 | 90 | } |
137 | 91 | } |
0 commit comments