44 */
55package org .hibernate .orm .test .envers ;
66
7- import java .util .Arrays ;
8- import java .util .Date ;
9- import java .util .HashSet ;
10- import java .util .Map ;
11- import java .util .Set ;
127import jakarta .persistence .Column ;
138import jakarta .persistence .ElementCollection ;
149import jakarta .persistence .Entity ;
1914import jakarta .persistence .Table ;
2015import jakarta .persistence .Temporal ;
2116import jakarta .persistence .TemporalType ;
22-
2317import org .hibernate .annotations .CreationTimestamp ;
2418import org .hibernate .cfg .AvailableSettings ;
25- import org .hibernate .engine .spi .SessionFactoryImplementor ;
2619import org .hibernate .envers .AuditReaderFactory ;
2720import org .hibernate .envers .Audited ;
28- import org .hibernate .envers .RevisionMapping ;
2921import org .hibernate .envers .ModifiedEntityNames ;
3022import org .hibernate .envers .RevisionEntity ;
23+ import org .hibernate .envers .RevisionMapping ;
3124import org .hibernate .jpa .boot .spi .Bootstrap ;
32-
3325import org .hibernate .testing .orm .junit .EntityManagerFactoryBasedFunctionalTest ;
34- import org .junit .Test ;
26+ import org .junit .jupiter .api .Test ;
27+
28+ import java .util .Arrays ;
29+ import java .util .Date ;
30+ import java .util .HashSet ;
31+ import java .util .Map ;
32+ import java .util .Set ;
33+
34+ import static org .assertj .core .api .Assertions .assertThat ;
3535
36- import static org .hibernate .testing .transaction .TransactionUtil .doInJPA ;
37- import static org .junit .Assert .assertEquals ;
3836
3937/**
4038 * @author Vlad Mihalcea
@@ -44,86 +42,78 @@ public class EntityTypeChangeAuditTest extends EntityManagerFactoryBasedFunction
4442@ Override
4543protected Class <?>[] getAnnotatedClasses () {
4644return new Class <?>[] {
47- Customer .class ,
48- CustomTrackingRevisionEntity .class
45+ Customer .class ,
46+ CustomTrackingRevisionEntity .class
4947};
5048}
5149
5250@ Test
5351public void test () {
54- final EntityManagerFactory testEmf = produceEntityManagerFactory ();
55-
56- doInJPA ( () -> testEmf , entityManager -> {
57- Customer customer = new Customer ();
58- customer .setId (1L );
59- customer .setFirstName ("John" );
60- customer .setLastName ("Doe" );
61-
62- entityManager .persist (customer );
63- });
64-
65- doInJPA ( () -> testEmf , entityManager -> {
66- //tag::envers-tracking-modified-entities-queries-example1[]
67- assertEquals (
68- "org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$Customer" ,
69- AuditReaderFactory
70- .get (entityManager )
71- .getCrossTypeRevisionChangesReader ()
72- .findEntityTypes (1 )
73- .iterator ().next ()
74- .getFirst ()
75- );
76- //end::envers-tracking-modified-entities-queries-example1[]
77- });
78-
79- EntityManagerFactory entityManagerFactory = null ;
80- try {
81- Map settings = buildSettings ();
82- settings .put (
52+ try (final EntityManagerFactory testEmf = produceEntityManagerFactory ()) {
53+
54+ testEmf .runInTransaction ( entityManager -> {
55+ Customer customer = new Customer ();
56+ customer .setId ( 1L );
57+ customer .setFirstName ( "John" );
58+ customer .setLastName ( "Doe" );
59+
60+ entityManager .persist ( customer );
61+ } );
62+
63+ testEmf .runInTransaction ( entityManager -> {
64+ //tag::envers-tracking-modified-entities-queries-example1[]
65+ assertThat (
66+ AuditReaderFactory
67+ .get ( entityManager )
68+ .getCrossTypeRevisionChangesReader ()
69+ .findEntityTypes ( 1 )
70+ .iterator ().next ()
71+ .getFirst ()
72+ ).isEqualTo ( "org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$Customer" );
73+ //end::envers-tracking-modified-entities-queries-example1[]
74+ } );
75+
76+ try (EntityManagerFactory entityManagerFactory = buildEntityManagerFactory ()) {
77+ final EntityManagerFactory emf = entityManagerFactory ;
78+
79+ entityManagerFactory .runInTransaction ( entityManager -> {
80+ ApplicationCustomer customer = entityManager .find ( ApplicationCustomer .class , 1L );
81+ customer .setLastName ( "Doe Jr." );
82+ } );
83+
84+ entityManagerFactory .runInTransaction ( entityManager -> {
85+ //tag::envers-tracking-modified-entities-queries-example2[]
86+ assertThat (
87+ AuditReaderFactory
88+ .get ( entityManager )
89+ .getCrossTypeRevisionChangesReader ()
90+ .findEntityTypes ( 2 )
91+ .iterator ().next ()
92+ .getFirst ()
93+ ).isEqualTo ( "org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$ApplicationCustomer" );
94+ //end::envers-tracking-modified-entities-queries-example2[]
95+ } );
96+ }
97+ }
98+ }
99+
100+ private EntityManagerFactory buildEntityManagerFactory () {
101+ Map <Object , Object > settings = buildSettings ();
102+ settings .put (
83103AvailableSettings .LOADED_CLASSES ,
84104Arrays .asList (
85- ApplicationCustomer .class ,
86- CustomTrackingRevisionEntity .class
87- )
88- );
89- settings .put (
90- AvailableSettings .HBM2DDL_AUTO ,
91- "update"
105+ ApplicationCustomer .class ,
106+ CustomTrackingRevisionEntity .class
107+ )
92108);
93- entityManagerFactory = Bootstrap .getEntityManagerFactoryBuilder (
94- new TestingPersistenceUnitDescriptorImpl (getClass ().getSimpleName ()),
95- settings
96- ).build ().unwrap (SessionFactoryImplementor .class );
97-
98- final EntityManagerFactory emf = entityManagerFactory ;
99-
100- doInJPA (() -> emf , entityManager -> {
101- ApplicationCustomer customer = entityManager .find (ApplicationCustomer .class , 1L );
102- customer .setLastName ("Doe Jr." );
103- });
104-
105- doInJPA (() -> emf , entityManager -> {
106- //tag::envers-tracking-modified-entities-queries-example2[]
107- assertEquals (
108- "org.hibernate.orm.test.envers.EntityTypeChangeAuditTest$ApplicationCustomer" ,
109- AuditReaderFactory
110- .get (entityManager )
111- .getCrossTypeRevisionChangesReader ()
112- .findEntityTypes (2 )
113- .iterator ().next ()
114- .getFirst ()
115- );
116- //end::envers-tracking-modified-entities-queries-example2[]
117- });
118- }
119- finally {
120- if (entityManagerFactory != null ) {
121- entityManagerFactory .close ();
122- }
123- testEmf .close ();
124- }
109+ settings .put ( AvailableSettings .HBM2DDL_AUTO , "update" );
110+ return Bootstrap .getEntityManagerFactoryBuilder (
111+ new TestingPersistenceUnitDescriptorImpl ( getClass ().getSimpleName () ),
112+ settings )
113+ .build ();
125114}
126115
116+
127117//tag::envers-tracking-modified-entities-revchanges-before-rename-example[]
128118@ Audited
129119@ Entity (name = "Customer" )
@@ -142,7 +132,7 @@ public static class Customer {
142132private Date createdOn ;
143133
144134//Getters and setters are omitted for brevity
145- //end::envers-tracking-modified-entities-revchanges-before-rename-example[]
135+ //end::envers-tracking-modified-entities-revchanges-before-rename-example[]
146136
147137public Long getId () {
148138return id ;
@@ -175,7 +165,7 @@ public Date getCreatedOn() {
175165public void setCreatedOn (Date createdOn ) {
176166this .createdOn = createdOn ;
177167}
178- //tag::envers-tracking-modified-entities-revchanges-before-rename-example[]
168+ //tag::envers-tracking-modified-entities-revchanges-before-rename-example[]
179169}
180170//end::envers-tracking-modified-entities-revchanges-before-rename-example[]
181171
@@ -197,7 +187,7 @@ public static class ApplicationCustomer {
197187private Date createdOn ;
198188
199189//Getters and setters are omitted for brevity
200- //end::envers-tracking-modified-entities-revchanges-after-rename-example[]
190+ //end::envers-tracking-modified-entities-revchanges-after-rename-example[]
201191
202192public Long getId () {
203193return id ;
@@ -230,7 +220,7 @@ public Date getCreatedOn() {
230220public void setCreatedOn (Date createdOn ) {
231221this .createdOn = createdOn ;
232222}
233- //tag::envers-tracking-modified-entities-revchanges-after-rename-example[]
223+ //tag::envers-tracking-modified-entities-revchanges-after-rename-example[]
234224}
235225//end::envers-tracking-modified-entities-revchanges-after-rename-example[]
236226
@@ -242,9 +232,9 @@ public static class CustomTrackingRevisionEntity extends RevisionMapping {
242232
243233@ ElementCollection
244234@ JoinTable (
245- name = "REVCHANGES" ,
246- joinColumns = @ JoinColumn (name = "REV" )
247- )
235+ name = "REVCHANGES" ,
236+ joinColumns = @ JoinColumn (name = "REV" )
237+ )
248238@ Column (name = "ENTITYNAME" )
249239@ ModifiedEntityNames
250240private Set <String > modifiedEntityNames = new HashSet <>();
0 commit comments