-
Notifications
You must be signed in to change notification settings - Fork 0
guide auditing
For database auditing we use hibernate envers. If you want to use auditing ensure you have the following dependency in your pom.xml:
<dependency>
<groupId>io.oasp.java.modules</groupId>
<artifactId>oasp4j-jpa-envers</artifactId>
</dependency>
Make sure that entity manager (configured in beans-jpa.xml) also scans the package from the oasp4j-jpa[-envers] module in order to work properly.
...
<property name="packagesToScan">
<list>
<value>io.oasp.module.jpa.dataaccess.api</value>
...
</list>
Now let your DAO implementation extend from AbstractRevisionedDao instead of AbstractDao and your DAO interface extend from [Application]RevisionedDao instead of [Application]Dao.
The DAO now has a method getRevisionHistory(entity) available to get a list of revisions for a given entity and a method load(id, revision) to load a specific revision of an entity with the given ID.
To enable auditing for a entity simply place the @Audited annotation to your entity and all entity classes it extends from.
@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 <ENTITY_NAME>_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).
database.hibernate.hbm2ddl.auto=create
Another possibility is to put them in your database migration scripts like so.
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.<TABLE_NAME>_AUD(
<ALL_TABLE_ATTRIBUTES>,
revtype TINYINT,
rev BIGINT NOT NULL
);
This documentation is licensed under the Creative Commons License (Attribution-NoDerivatives 4.0 International).