Skip to content

Commit 0378e59

Browse files
mbelladesebersole
authored andcommitted
HHH-17612 HHH-18762 Migration guide and Envers documentation updates
1 parent e06da88 commit 0378e59

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

documentation/src/main/asciidoc/userguide/chapters/envers/Envers.adoc

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,16 @@ Only used if the `ValidityAuditStrategy` is used, and `org.hibernate.envers.audi
270270
When set to `true`, the legacy mapping behavior is used such that the revision end timestamp is only maintained in the root entity audit table.
271271
When set to `false`, the revision end timestamp is maintained in both the root entity and joined subclass audit tables; allowing the potential to apply database partitioning to the joined subclass tables just like the root entity audit tables.
272272

273+
[[envers-config-native-id]]
273274
`*org.hibernate.envers.use_revision_entity_with_native_id*` (default: `true` )::
274275
Boolean flag that determines the strategy of revision number generation.
275276
Default implementation of revision entity uses native identifier generator.
276277
+
277278
If the current database engine does not support identity columns, users are advised to set this property to false.
278279
+
279-
In this case revision numbers are created by preconfigured `org.hibernate.id.enhanced.SequenceStyleGenerator`.
280-
See: `org.hibernate.envers.DefaultRevisionEntity` and `org.hibernate.envers.enhanced.SequenceIdRevisionEntity`.
280+
In this case revision numbers are created by a preconfigured `org.hibernate.id.enhanced.SequenceStyleGenerator`.
281281

282+
[[envers-config-track-entities]]
282283
`*org.hibernate.envers.track_entities_changed_in_revision*` (default: `false` )::
283284
Should entity types, that have been modified during each revision, be tracked.
284285
The default implementation creates `REVCHANGES` table that stores entity names of modified persistent objects.
@@ -484,18 +485,49 @@ Either a `long/Long` or `java.util.Date` value representing the instant at which
484485
When using a `java.util.Date`, instead of a `long/Long` for the revision timestamp, take care not to store it to a column data type which will lose precision.
485486

486487
Envers handles this information as an entity.
487-
By default it uses its own internal class to act as the entity, mapped to the `REVINFO` table.
488-
You can, however, supply your own approach to collecting this information which might be useful to capture additional details such as who made a change
488+
489+
[[envers-default-revision-entity]]
490+
==== Default Revision Entity
491+
492+
By default, Envers uses its own internal class to act as the entity, mapped to the `REVINFO` table.
493+
The entity type that's used depends on a couple configuration properties: <<envers-config-native-id,native identifiers>> and <<envers-config-track-entities, entity tracking>>. Here is a table showing the entity type used based on the configuration values:
494+
[cols="1,1,1"]
495+
|===
496+
|
497+
| native-id `false`
498+
| native-id `true`
499+
500+
| track-entities `false`
501+
| `org.hibernate.envers.DefaultRevisionEntity`
502+
| `org.hibernate.envers.enhanced.SequenceIdRevisionEntity`
503+
504+
| track-entities `true`
505+
| `org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity`
506+
| `org.hibernate.envers.enhanced.SequenceIdTrackingModifiedEntitiesRevisionEntity`
507+
|===
508+
509+
[[envers-custom-revision-entity]]
510+
==== Custom Revision Entity
511+
512+
You can also supply your own approach to collecting this information which might be useful to capture additional details such as who made a change
489513
or the IP address from which the request came.
490514
There are two things you need to make this work:
491515

492516
. First, you will need to tell Envers about the entity you wish to use.
493517
Your entity must use the `@org.hibernate.envers.RevisionEntity` annotation.
494518
It must define the two attributes described above annotated with `@org.hibernate.envers.RevisionNumber` and `@org.hibernate.envers.RevisionTimestamp`, respectively.
495-
You can extend from `org.hibernate.envers.DefaultRevisionEntity`, if you wish, to inherit all these required behaviors.
519+
You can extend from any of the revision mapped superclass types, if you wish, to inherit all these required behaviors:
520+
521+
org.hibernate.envers.RevisionMapping
522+
org.hibernate.envers.TrackingModifiedEntitiesRevisionMapping
523+
org.hibernate.envers.enhanced.SequenceIdRevisionMapping
524+
org.hibernate.envers.enhanced.SequenceIdTrackingModifiedEntitiesRevisionMapping
525+
496526
+
497527
Simply add the custom revision entity as you do your normal entities and Envers will *find it*.
498528
+
529+
To understand which mapping you should extend based on configuration see the <<envers-default-revision-entity,default revision entity>> paragraph.
530+
+
499531
NOTE: It is an error for there to be multiple entities marked as `@org.hibernate.envers.RevisionEntity`.
500532

501533
. Second, you need to tell Envers how to create instances of your revision entity which is handled by the
@@ -1003,7 +1035,7 @@ If true, the result of the query will be a list of entities (which changed at re
10031035
If false, the result will be a list of three element arrays:
10041036

10051037
* the first element will be the changed entity instance.
1006-
* the second will be an entity containing revision data (if no custom entity is used, this will be an instance of `DefaultRevisionEntity`).
1038+
* the second will be an entity containing revision data (if no custom entity is used, this will be an instance of the <<envers-default-revision-entity,default revision entity type>>).
10071039
* the third will be the type of the revision (one of the values of the `RevisionType` enumeration: `ADD`, `MOD`, `DEL`).
10081040

10091041
`selectDeletedEntities`:: The second parameter specifies if revisions,
@@ -1330,17 +1362,31 @@ Here is a simple example:
13301362
[source,java]
13311363
----
13321364
AuditQuery query = getAuditReader().createQuery()
1333-
.forRevisionsOfEntity( DefaultRevisionEntity.class, true )
1365+
.forRevisionsOfEntity( Customer.class, true )
13341366
.add( AuditEntity.revisionNumber().between( 1, 25 ) );
13351367
----
13361368

1337-
This query will return all revision information entities for revisions between 1 and 25 including those which are
1369+
This query will return all information for revisions between 1 and 25 including those which are
13381370
related to deletions. If deletions are not of interest, you would pass `false` as the second argument.
13391371

1340-
Note that this query uses the `DefaultRevisionEntity` class type. The class provided will vary depending on the
1341-
configuration properties used to configure Envers or if you supply your own revision entity. Typically users who
1342-
will use this API will likely be providing a custom revision entity implementation to obtain custom information
1343-
being maintained per revision.
1372+
Note that this query produces `@RevisionEntity` instances. The obtained instance type will vary depending on the
1373+
configuration properties used to configure Envers, like showed in <<envers-default-revision-entity,this paragraph>>,
1374+
or if you supply your own revision entity.
1375+
1376+
[[envers-querying-revision-info]]
1377+
=== Directly querying revision information
1378+
1379+
You can also directly query all revision information available on the database by writing HQL or Criteria queries
1380+
which select from the revision entity used by your application. For example:
1381+
1382+
[source,java]
1383+
----
1384+
List<DefaultRevisionEntity> resultList = session.createQuery( "from DefaultRevisionEntity where id = 1", DefaultRevisionEntity.class ).getResultList();
1385+
----
1386+
1387+
This query will return all revision entity information for revision numbers equal to 1 (the first revision of each entity).
1388+
Often, users who will take advantage of this functionality will be providing a custom revision entity implementation to
1389+
obtain additional information being maintained per revision.
13441390

13451391
[[envers-conditional-auditing]]
13461392
=== Conditional auditing

migration-guide.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,34 @@ Previous versions allowed some questionable (at best) attribute naming patterns.
122122
String isDefault();
123123
----
124124

125+
[[envers-rev-types]]
126+
== Hibernate Envers and custom revision entities
127+
128+
Users that wanted to customize the `@RevisionEntity` used by Envers could do so by extending one on the four default revision entity types:
129+
130+
[source]
131+
----
132+
org.hibernate.envers.DefaultRevisionEntity
133+
org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity
134+
org.hibernate.envers.enhanced.SequenceIdRevisionEntity
135+
org.hibernate.envers.enhanced.SequenceIdTrackingModifiedEntitiesRevisionEntity
136+
----
137+
138+
These types are annotated with `@MappedSuperclass` to enable this custom extension. When no custom revision entity was specified, though,
139+
the same class was mapped as an entity type by Envers internals. This caused problems when dealing with the domain metamodel and static
140+
metamodel aspect of these types, so we chose to create *new separate classes* annotated `@MappedSuperclass` from which revision entities,
141+
meaning the default ones as well as yours, *should extend from*. These types are (in the same order):
142+
143+
[source]
144+
----
145+
org.hibernate.envers.RevisionMapping
146+
org.hibernate.envers.TrackingModifiedEntitiesRevisionMapping
147+
org.hibernate.envers.enhanced.SequenceIdRevisionMapping
148+
org.hibernate.envers.enhanced.SequenceIdTrackingModifiedEntitiesRevisionMapping
149+
----
150+
151+
Also, you can now write HQL queries using the simple class name of default revision entities to retrieve all revision information.
152+
Find out more in link:{user-guide-url}#envers-querying-revision-info[this user guide chapter].
125153

126154
[[create-query]]
127155
== Queries with implicit `select` list and no explicit result type

0 commit comments

Comments
 (0)