:toc: macro
toc::[]
= Auditing
For database auditing we use http://envers.jboss.org/[hibernate envers]. If you want to use auditing ensure you have the following dependency in your +pom.xml+:
.**spring**
[source,xml]
----
com.devonfw.java.modules
devon4j-jpa-envers
----
.**quarkus**
[source,xml]
----
io.quarkus
quarkus-hibernate-envers
----
NOTE: The following part applies only to spring applications. At this point, the Quarkus extension does not provide any additional configurations. For Quarkus applications, simply use the `@Audited` annotation to enable auditing for an entity class, as described a few lines below or seen https://hibernate.org/orm/envers/[here].
Make sure that entity manager also scans the package from the +devon4j-jpa[-envers]+ module in order to work properly. And make sure that correct Repository Factory Bean Class is chosen.
[source,java]
----
@EntityScan(basePackages = { "«my.base.package»" }, basePackageClasses = { AdvancedRevisionEntity.class })
...
@EnableJpaRepositories(repositoryFactoryBeanClass = GenericRevisionedRepositoryFactoryBean.class)
...
public class SpringBootApp {
...
}
----
Now let your [Entity]Repository extend from +DefaultRevisionedRepository+ instead of +DefaultRepository+.
The repository now has a method +getRevisionHistoryMetadata(id)+ and +getRevisionHistoryMetadata(id, boolean lazy)+ available to get a list of revisions for a given entity and a method +find(id, revision)+ to load a specific revision of an entity with the given ID or getLastRevisionHistoryMetadata(id) to load last revision.
//Auditing is not used anymore
To enable auditing for a entity simply place the +@Audited+ annotation to your entity and all entity classes it extends from.
[source,java]
----
@Entity(name = "Drink")
@Audited
public class DrinkEntity extends ProductEntity implements Drink {
...
----
When auditing is enabled for an entity an additional database table is used to store all changes to the entity table and a corresponding revision number. This table is called +_AUD+ per default. Another table called +REVINFO+ is used to store all revisions. Make sure that these tables are available. They can be generated by hibernate with the following property (only for development environments).
[source, properties]
----
database.hibernate.hbm2ddl.auto=create
----
Another possibility is to put them in your link:guide-database-migration[database migration] scripts like so.
[source, sql]
----
CREATE CACHED TABLE PUBLIC.REVINFO(
id BIGINT NOT NULL generated by default as identity (start with 1),
timestamp BIGINT NOT NULL,
user VARCHAR(255)
);
...
CREATE CACHED TABLE PUBLIC._AUD(
,
revtype TINYINT,
rev BIGINT NOT NULL
);
----