44 */
55package org .hibernate .orm .test .envers .integration .flush ;
66
7- import static org .junit .Assert .assertEquals ;
7+ import static org .junit .jupiter . api . Assertions .assertEquals ;
88
99import java .util .Arrays ;
1010import java .util .Date ;
1414import jakarta .persistence .CascadeType ;
1515import jakarta .persistence .Column ;
1616import jakarta .persistence .Entity ;
17- import jakarta .persistence .EntityManager ;
1817import jakarta .persistence .FetchType ;
1918import jakarta .persistence .FlushModeType ;
2019import jakarta .persistence .GeneratedValue ;
2726
2827import org .hibernate .envers .AuditMappedBy ;
2928import org .hibernate .envers .Audited ;
30- import org .hibernate .orm .test .envers .BaseEnversJPAFunctionalTestCase ;
31- import org .hibernate .orm .test .envers .Priority ;
29+ import org .hibernate .envers .AuditReaderFactory ;
30+ import org .hibernate .testing .envers .junit .EnversTest ;
31+ import org .hibernate .testing .orm .junit .BeforeClassTemplate ;
32+ import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
3233import org .hibernate .testing .orm .junit .JiraKey ;
3334import org .hibernate .testing .orm .junit .JiraKeyGroup ;
34- import org .junit .Test ;
35+ import org .hibernate .testing .orm .junit .Jpa ;
36+ import org .junit .jupiter .api .Test ;
3537
3638/**
3739 * @author Chris Cranford
4042 @ JiraKey (value = "HHH-12826" ),
4143 @ JiraKey (value = "HHH-12846" )
4244} )
43- public class CommitFlushCollectionTest extends BaseEnversJPAFunctionalTestCase {
45+ @ EnversTest
46+ @ Jpa (annotatedClasses = {CommitFlushCollectionTest .DocumentA .class , CommitFlushCollectionTest .DocumentLineA .class })
47+ public class CommitFlushCollectionTest {
4448
4549 @ MappedSuperclass
4650 public static abstract class AbstractEntity {
@@ -147,17 +151,28 @@ public void setDocument(DocumentA document) {
147151 }
148152 }
149153
150- @ Override
151- protected Class <?>[] getAnnotatedClasses () {
152- return new Class <?>[] { DocumentA .class , DocumentLineA .class };
154+ private Long entityId1 ;
155+ private Long entityId2 ;
156+
157+ @ BeforeClassTemplate
158+ public void initData (EntityManagerFactoryScope scope ) {
159+ // This failed when using Envers.
160+ entityId1 = persistDocument ( scope , FlushModeType .COMMIT );
161+
162+ // This worked
163+ entityId2 = persistDocument ( scope , FlushModeType .AUTO );
164+
165+ // This failed
166+ mergeDocument ( scope , FlushModeType .COMMIT , entityId1 );
167+
168+ // This worked
169+ mergeDocument ( scope , FlushModeType .AUTO , entityId2 );
153170 }
154171
155- private Long persistDocument (FlushModeType flushModeType ) {
156- final EntityManager entityManager = getOrCreateEntityManager ();
157- try {
158- entityManager .setFlushMode ( flushModeType );
172+ private Long persistDocument (EntityManagerFactoryScope scope , FlushModeType flushModeType ) {
173+ return scope .fromTransaction ( em -> {
174+ em .setFlushMode ( flushModeType );
159175
160- entityManager .getTransaction ().begin ();
161176 DocumentA doc = new DocumentA ();
162177 doc .setNumber ( "1" );
163178 doc .setDate ( new Date () );
@@ -166,31 +181,16 @@ private Long persistDocument(FlushModeType flushModeType) {
166181 line .setText ( "line1" );
167182 doc .addLine ( line );
168183
169- entityManager .persist ( doc );
170- entityManager .getTransaction ().commit ();
171-
184+ em .persist ( doc );
172185 return doc .getId ();
173- }
174- catch ( Exception e ) {
175- if ( entityManager != null && entityManager .getTransaction ().isActive () ) {
176- entityManager .getTransaction ().rollback ();
177- }
178- throw e ;
179- }
180- finally {
181- if ( entityManager != null && entityManager .isOpen () ) {
182- entityManager .close ();
183- }
184- }
186+ } );
185187 }
186188
187- private void mergeDocument (FlushModeType flushModeType , Long id ) {
188- final EntityManager entityManager = getOrCreateEntityManager ();
189- try {
190- entityManager .setFlushMode ( flushModeType );
189+ private void mergeDocument (EntityManagerFactoryScope scope , FlushModeType flushModeType , Long id ) {
190+ scope .inTransaction ( em -> {
191+ em .setFlushMode ( flushModeType );
191192
192- entityManager .getTransaction ().begin ();
193- DocumentA doc = entityManager .find ( DocumentA .class , id );
193+ DocumentA doc = em .find ( DocumentA .class , id );
194194 doc .setDate ( new Date () );
195195 for ( DocumentLineA line : doc .getLines () ) {
196196 line .setText ( "Updated" );
@@ -200,49 +200,23 @@ private void mergeDocument(FlushModeType flushModeType, Long id) {
200200 line .setText ( "line2" );
201201 doc .addLine ( line );
202202
203- entityManager .merge ( doc );
204- entityManager .getTransaction ().commit ();
205- }
206- catch ( Exception e ) {
207- if ( entityManager != null && entityManager .getTransaction ().isActive () ) {
208- entityManager .getTransaction ().rollback ();
209- }
210- throw e ;
211- }
212- finally {
213- if ( entityManager != null && entityManager .isOpen () ) {
214- entityManager .close ();
215- }
216- }
217- }
218-
219- private Long entityId1 ;
220- private Long entityId2 ;
221-
222- @ Test
223- @ Priority (10 )
224- public void initData () {
225- // This failed when using Envers.
226- entityId1 = persistDocument ( FlushModeType .COMMIT );
227-
228- // This worked
229- entityId2 = persistDocument ( FlushModeType .AUTO );
230-
231- // This failed
232- mergeDocument ( FlushModeType .COMMIT , entityId1 );
233-
234- // This worked
235- mergeDocument ( FlushModeType .AUTO , entityId2 );
203+ em .merge ( doc );
204+ } );
236205 }
237206
238207 @ Test
239- public void testWithFlushModeCommit () {
240- assertEquals ( Arrays .asList ( 1 , 3 ), getAuditReader ().getRevisions ( DocumentA .class , entityId1 ) );
208+ public void testWithFlushModeCommit (EntityManagerFactoryScope scope ) {
209+ scope .inEntityManager ( em -> {
210+ assertEquals ( Arrays .asList ( 1 , 3 ),
211+ AuditReaderFactory .get ( em ).getRevisions ( DocumentA .class , entityId1 ) );
212+ } );
241213 }
242214
243215 @ Test
244- @ Priority (1 )
245- public void testWithFlushModeAuto () {
246- assertEquals ( Arrays .asList ( 2 , 4 ), getAuditReader ().getRevisions ( DocumentA .class , entityId2 ) );
216+ public void testWithFlushModeAuto (EntityManagerFactoryScope scope ) {
217+ scope .inEntityManager ( em -> {
218+ assertEquals ( Arrays .asList ( 2 , 4 ),
219+ AuditReaderFactory .get ( em ).getRevisions ( DocumentA .class , entityId2 ) );
220+ } );
247221 }
248222}
0 commit comments