7. What they say about…
“The Envers project aims to enable
easy auditing/versioning of persistent
classes […]”
Ref: http://www.jboss.org/envers
8. What they say about…
“[…] All that you have to do is
annotate your persistent class or
some of its properties, that you want
to audit, with @Audited. […]”
Ref: http://www.jboss.org/envers
9. What they say about…
“[…] For each audited entity, a table
will be created, which will hold the
history of changes made to the
entity. […]”
Ref: http://www.jboss.org/envers
10. And the best part…
“[…] You can then retrieve and query
historical data without much effort.”
Ref: http://www.jboss.org/envers
http://www.deviantart.com
11. Some features:
Auditing of all mappings defined by
the JPA specification
Auditing of some Hibernate
mappings, which extend JPA
Logging data for each revision using
a "revision entity"
Querying historical data
Ref: http://www.jboss.org/envers
http://www.deviantart.com
14. And how do we use in the past?
<property name="hibernate.ejb.event.post-insert"
value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernat
e.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-update"
value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hiberna
te.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-delete"
value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernat
e.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.pre-collection-update"
value="org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.pre-collection-remove"
value="org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-collection-recreate"
value="org.hibernate.envers.event.AuditEventListener" />
In persistence.xml or hibernate.cfg.xml
15. And how to use?
<!-- Add on classpath | Maven -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.x.Final</version>
</dependency>
Requirements:
Hibernate 3+
Hibernate annotations
16. Simple sample
@Entity
public class Adress {
@Id
@GeneratedValue
private int id;
@Audited
private String streetName;
@Audited
private Long zipCode;
@Audited
@ManyToOne
private State state;
}
17. The whole entity
@Audited
public class Adress {
@Id
@GeneratedValue
private int id;
private String streetName;
private Long zipCode;
@ManyToOne
private State state;
}
@Audited // Here too!
public class State {
@Id
@GeneratedValue
private int id;
private String stateName;
private Long population;
}
18. … without a field
@Audited
public class Adress {
@Id
@GeneratedValue
private int id;
@NotAudited
private String streetName;
private Long zipCode;
@ManyToOne
private State state;
}
19. Changing the entity table
name
@Audited
@AuditTable(“TB_ADRESS_AUDIT”)
public class Adress {
@Id
@GeneratedValue
private int id;
@NotAudited
private String streetName;
private Long zipCode;
@ManyToOne
private State state;
}
21. Querying for entities of a
class at a given revision
AuditQuery query =
getAuditReader().createQuery()
.forEntitiesAtRevision(MyEntity.class,
revisionNumber);
22. Querying for revisions, at
which entities of a given class
changed
AuditQuery query =
getAuditReader().createQuery()
.forRevisionsOfEntity(MyEntity.class,
false, true);
24. Configuration Properties
// Prefixes and suffixes
org.hibernate.envers.audit_table_prefix
org.hibernate.envers.audit_table_suffix
// Revision number and revision type fields
org.hibernate.envers.revision_field_name
org.hibernate.envers.revision_type_field_name
// Default schema used
org.hibernate.envers.default_schema