|
6 | 6 | */ |
7 | 7 | package org.hibernate.test.generatedkeys.identity; |
8 | 8 |
|
| 9 | +import javax.persistence.PersistenceException; |
| 10 | +import javax.persistence.TransactionRequiredException; |
| 11 | + |
9 | 12 | import org.junit.Test; |
10 | 13 |
|
11 | 14 | import org.hibernate.Session; |
|
15 | 18 | import org.hibernate.testing.RequiresDialectFeature; |
16 | 19 | import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
17 | 20 |
|
| 21 | +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; |
18 | 22 | import static org.junit.Assert.assertEquals; |
19 | 23 | import static org.junit.Assert.assertNotNull; |
20 | 24 | import static org.junit.Assert.assertNull; |
| 25 | +import static org.junit.Assert.fail; |
21 | 26 |
|
22 | 27 | /** |
23 | 28 | * @author Steve Ebersole |
@@ -49,119 +54,123 @@ public void testIdentityColumnGeneratedIds() { |
49 | 54 | @Test |
50 | 55 | public void testPersistOutsideTransaction() { |
51 | 56 | Session s = openSession(); |
52 | | - |
53 | | - // first test save() which should force an immediate insert... |
54 | | - MyEntity myEntity1 = new MyEntity( "test-save" ); |
55 | | - Long id = ( Long ) s.save( myEntity1 ); |
56 | | - assertNotNull( "identity column did not force immediate insert", id ); |
57 | | - assertEquals( id, myEntity1.getId() ); |
58 | | - |
59 | | - // next test persist() which should cause a delayed insert... |
60 | | - long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount(); |
61 | | - MyEntity myEntity2 = new MyEntity( "test-persist"); |
62 | | - s.persist( myEntity2 ); |
63 | | - assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
64 | | - assertNull( myEntity2.getId() ); |
65 | | - |
66 | | - // an explicit flush should cause execution of the delayed insertion |
67 | | - s.flush(); |
68 | | - assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 1, sessionFactory().getStatistics().getEntityInsertCount() ); |
69 | | - s.close(); |
70 | | - |
71 | | - s = openSession(); |
72 | | - s.beginTransaction(); |
73 | | - s.delete( myEntity1 ); |
74 | | - s.delete( myEntity2 ); |
75 | | - s.getTransaction().commit(); |
76 | | - s.close(); |
| 57 | + try { |
| 58 | + // first test save() which should force an immediate insert... |
| 59 | + MyEntity myEntity1 = new MyEntity( "test-save" ); |
| 60 | + Long id = (Long) s.save( myEntity1 ); |
| 61 | + assertNotNull( "identity column did not force immediate insert", id ); |
| 62 | + assertEquals( id, myEntity1.getId() ); |
| 63 | + |
| 64 | + // next test persist() which should cause a delayed insert... |
| 65 | + long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount(); |
| 66 | + MyEntity myEntity2 = new MyEntity( "test-persist" ); |
| 67 | + s.persist( myEntity2 ); |
| 68 | + assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
| 69 | + assertNull( myEntity2.getId() ); |
| 70 | + |
| 71 | + // an explicit flush should cause execution of the delayed insertion |
| 72 | + s.flush(); |
| 73 | + fail( "TransactionRequiredException required upon flush" ); |
| 74 | + } |
| 75 | + catch ( PersistenceException ex ) { |
| 76 | + // expected |
| 77 | + assertTyping( TransactionRequiredException.class, ex ); |
| 78 | + } |
| 79 | + finally { |
| 80 | + s.close(); |
| 81 | + } |
77 | 82 | } |
78 | 83 |
|
79 | 84 | @Test |
80 | 85 | @SuppressWarnings( {"unchecked"}) |
81 | 86 | public void testPersistOutsideTransactionCascadedToNonInverseCollection() { |
82 | 87 | long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount(); |
83 | 88 | Session s = openSession(); |
84 | | - MyEntity myEntity = new MyEntity( "test-persist"); |
85 | | - myEntity.getNonInverseChildren().add( new MyChild( "test-child-persist-non-inverse" ) ); |
86 | | - s.persist( myEntity ); |
87 | | - assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
88 | | - assertNull( myEntity.getId() ); |
89 | | - s.flush(); |
90 | | - assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() ); |
91 | | - s.close(); |
92 | | - |
93 | | - s = openSession(); |
94 | | - s.beginTransaction(); |
95 | | - s.createQuery( "delete MyChild" ).executeUpdate(); |
96 | | - s.createQuery( "delete MyEntity" ).executeUpdate(); |
97 | | - s.getTransaction().commit(); |
98 | | - s.close(); |
| 89 | + try { |
| 90 | + MyEntity myEntity = new MyEntity( "test-persist" ); |
| 91 | + myEntity.getNonInverseChildren().add( new MyChild( "test-child-persist-non-inverse" ) ); |
| 92 | + s.persist( myEntity ); |
| 93 | + assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
| 94 | + assertNull( myEntity.getId() ); |
| 95 | + s.flush(); |
| 96 | + fail( "TransactionRequiredException required upon flush" ); |
| 97 | + } |
| 98 | + catch ( PersistenceException ex ) { |
| 99 | + // expected |
| 100 | + assertTyping( TransactionRequiredException.class, ex ); |
| 101 | + } |
| 102 | + finally { |
| 103 | + s.close(); |
| 104 | + } |
99 | 105 | } |
100 | 106 |
|
101 | 107 | @Test |
102 | 108 | @SuppressWarnings( {"unchecked"}) |
103 | 109 | public void testPersistOutsideTransactionCascadedToInverseCollection() { |
104 | 110 | long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount(); |
105 | 111 | Session s = openSession(); |
106 | | - MyEntity myEntity2 = new MyEntity( "test-persist-2"); |
107 | | - MyChild child = new MyChild( "test-child-persist-inverse" ); |
108 | | - myEntity2.getInverseChildren().add( child ); |
109 | | - child.setInverseParent( myEntity2 ); |
110 | | - s.persist( myEntity2 ); |
111 | | - assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
112 | | - assertNull( myEntity2.getId() ); |
113 | | - s.flush(); |
114 | | - assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() ); |
115 | | - s.close(); |
116 | | - |
117 | | - s = openSession(); |
118 | | - s.beginTransaction(); |
119 | | - s.createQuery( "delete MyChild" ).executeUpdate(); |
120 | | - s.createQuery( "delete MyEntity" ).executeUpdate(); |
121 | | - s.getTransaction().commit(); |
122 | | - s.close(); |
| 112 | + try { |
| 113 | + MyEntity myEntity2 = new MyEntity( "test-persist-2" ); |
| 114 | + MyChild child = new MyChild( "test-child-persist-inverse" ); |
| 115 | + myEntity2.getInverseChildren().add( child ); |
| 116 | + child.setInverseParent( myEntity2 ); |
| 117 | + s.persist( myEntity2 ); |
| 118 | + assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
| 119 | + assertNull( myEntity2.getId() ); |
| 120 | + s.flush(); |
| 121 | + fail( "TransactionRequiredException expected upon flush." ); |
| 122 | + } |
| 123 | + catch ( PersistenceException ex ) { |
| 124 | + // expected |
| 125 | + assertTyping( TransactionRequiredException.class, ex ); |
| 126 | + } |
| 127 | + finally { |
| 128 | + s.close(); |
| 129 | + } |
123 | 130 | } |
124 | 131 |
|
125 | 132 | @Test |
126 | 133 | public void testPersistOutsideTransactionCascadedToManyToOne() { |
127 | 134 | long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount(); |
128 | 135 | Session s = openSession(); |
129 | | - MyEntity myEntity = new MyEntity( "test-persist"); |
130 | | - myEntity.setSibling( new MySibling( "test-persist-sibling-out" ) ); |
131 | | - s.persist( myEntity ); |
132 | | - assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
133 | | - assertNull( myEntity.getId() ); |
134 | | - s.flush(); |
135 | | - assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() ); |
136 | | - s.close(); |
137 | | - |
138 | | - s = openSession(); |
139 | | - s.beginTransaction(); |
140 | | - s.createQuery( "delete MyEntity" ).executeUpdate(); |
141 | | - s.createQuery( "delete MySibling" ).executeUpdate(); |
142 | | - s.getTransaction().commit(); |
143 | | - s.close(); |
| 136 | + try { |
| 137 | + MyEntity myEntity = new MyEntity( "test-persist" ); |
| 138 | + myEntity.setSibling( new MySibling( "test-persist-sibling-out" ) ); |
| 139 | + s.persist( myEntity ); |
| 140 | + assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
| 141 | + assertNull( myEntity.getId() ); |
| 142 | + s.flush(); |
| 143 | + fail( "TransactionRequiredException expected upon flush." ); |
| 144 | + } |
| 145 | + catch ( PersistenceException ex ) { |
| 146 | + // expected |
| 147 | + assertTyping( TransactionRequiredException.class, ex ); |
| 148 | + } |
| 149 | + finally { |
| 150 | + s.close(); |
| 151 | + } |
144 | 152 | } |
145 | 153 |
|
146 | 154 | @Test |
147 | 155 | public void testPersistOutsideTransactionCascadedFromManyToOne() { |
148 | 156 | long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount(); |
149 | 157 | Session s = openSession(); |
150 | | - MyEntity myEntity2 = new MyEntity( "test-persist-2"); |
151 | | - MySibling sibling = new MySibling( "test-persist-sibling-in" ); |
152 | | - sibling.setEntity( myEntity2 ); |
153 | | - s.persist( sibling ); |
154 | | - assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
155 | | - assertNull( myEntity2.getId() ); |
156 | | - s.flush(); |
157 | | - assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() ); |
158 | | - s.close(); |
159 | | - |
160 | | - s = openSession(); |
161 | | - s.beginTransaction(); |
162 | | - s.createQuery( "delete MySibling" ).executeUpdate(); |
163 | | - s.createQuery( "delete MyEntity" ).executeUpdate(); |
164 | | - s.getTransaction().commit(); |
165 | | - s.close(); |
| 158 | + try { |
| 159 | + MyEntity myEntity2 = new MyEntity( "test-persist-2" ); |
| 160 | + MySibling sibling = new MySibling( "test-persist-sibling-in" ); |
| 161 | + sibling.setEntity( myEntity2 ); |
| 162 | + s.persist( sibling ); |
| 163 | + assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() ); |
| 164 | + assertNull( myEntity2.getId() ); |
| 165 | + s.flush(); |
| 166 | + fail( "TransactionRequiredException expected upon flush." ); |
| 167 | + } |
| 168 | + catch ( PersistenceException ex ) { |
| 169 | + // expected |
| 170 | + assertTyping( TransactionRequiredException.class, ex ); |
| 171 | + } |
| 172 | + finally { |
| 173 | + s.close(); |
| 174 | + } |
166 | 175 | } |
167 | 176 | } |
0 commit comments