Skip to content

Commit

Permalink
Update auditing documentation.
Browse files Browse the repository at this point in the history
Mention that even when only using the CreatedDate & LastModifiedDate annotations it is mandatory to enable auditing.
Add sample of using auditing metadata within an embedded entity.

Closes #2283
Original pull request: #2285
  • Loading branch information
christophstrobl authored and mp911de committed Feb 9, 2021
1 parent 6fe2c3a commit 1227e43
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/main/asciidoc/auditing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
[[auditing.basics]]
== Basics
Spring Data provides sophisticated support to transparently keep track of who created or changed an entity and when the change happened. To benefit from that functionality, you have to equip your entity classes with auditing metadata that can be defined either using annotations or by implementing an interface.
Additionally auditing has to be enabled either via Java or XML configuration which ensures required infrastructure components get registered.
Please refer to the store specific section for configuration samples.

[NOTE]
====
Applications that only track creation and modification dates do not need to specify an <<auditing.auditor-aware>>.
====

[[auditing.annotations]]
=== Annotation-based Auditing Metadata
Expand All @@ -19,7 +26,7 @@ class Customer {
private User user;
@CreatedDate
private DateTime createdDate;
private Instant createdDate;
// … further properties omitted
}
Expand All @@ -28,6 +35,32 @@ class Customer {

As you can see, the annotations can be applied selectively, depending on which information you want to capture. The annotations capturing when changes were made can be used on properties of type Joda-Time, `DateTime`, legacy Java `Date` and `Calendar`, JDK8 date and time types, and `long` or `Long`.

Auditing metadata does not necessarily need to live in the root level entity but can be added to an embedded one (depending on the actual store in use), as shown in the snipped below.

.Audit metadata in embedded entity
====
[source, java]
----
class Customer {
@Embedded
private AuditMetadata auditingMetadata;
// … further properties omitted
}
class AuditMetadata {
@CreatedBy
private User user;
@CreatedDate
private Instant createdDate;
}
----
====

[[auditing.interfaces]]
=== Interface-based Auditing Metadata
In case you do not want to use annotations to define auditing metadata, you can let your domain class implement the `Auditable` interface. It exposes setter methods for all of the auditing properties.
Expand Down

0 comments on commit 1227e43

Please sign in to comment.