Envers + Seam + JBoss: Whyto use?Diogo Souza
Envers + Seam + JBoss:Why to use?Better...
The handshake
Diogo SouzaSystems Analyst at Indra Companyhttp://3adiX.com.brhttp://diogosouza.com.br@diogo_souza_c
Ah, the Envers!Once upon a time, in 2009…
What they say about…“The Envers project aims to enableeasy auditing/versioning of persistentclasses […]”Ref: http://www.jb...
What they say about…“[…] All that you have to do isannotate your persistent class orsome of its properties, that you wantt...
What they say about…“[…] For each audited entity, a tablewill be created, which will hold thehistory of changes made to th...
And the best part…“[…] You can then retrieve and queryhistorical data without much effort.”Ref: http://www.jboss.org/enver...
Some features: Auditing of all mappings defined bythe JPA specification Auditing of some Hibernatemappings, which extend...
BasicallyEach transaction......One revisionINSERTUPDATEDELETE
Where to use?Wherever you have Hibernate! Standalone JBoss AS Spring
And how do we use in the past?<property name="hibernate.ejb.event.post-insert"value="org.hibernate.ejb.event.EJB3PostInser...
And how to use?<!-- Add on classpath | Maven --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-envers</...
Simple sample@Entitypublic class Adress {@Id@GeneratedValueprivate int id;@Auditedprivate String streetName;@Auditedprivat...
The whole entity@Auditedpublic class Adress {@Id@GeneratedValueprivate int id;private String streetName;private Long zipCo...
… without a field@Auditedpublic class Adress {@Id@GeneratedValueprivate int id;@NotAuditedprivate String streetName;privat...
Changing the entity tablename@Audited@AuditTable(“TB_ADRESS_AUDIT”)public class Adress {@Id@GeneratedValueprivate int id;@...
The database0 ADD1 MOD3 DELThe revisiondate!
Querying for entities of aclass at a given revisionAuditQuery query =getAuditReader().createQuery().forEntitiesAtRevision(...
Querying for revisions, atwhich entities of a given classchangedAuditQuery query =getAuditReader().createQuery().forRevisi...
Querying for revisionsbetween revision datesAuditReader auditReader =AuditReaderFactory.get(entityManager);AuditQuery quer...
Configuration Properties// Prefixes and suffixesorg.hibernate.envers.audit_table_prefixorg.hibernate.envers.audit_table_su...
Demo Time!
I am not responsible forany increases in thenumber of Brazilianpunishments...
The guy!Adam WarskiRef: http://www.warski.org/blog/about/http://warski.org
Good things! Easy! Data as always! Database schemadoesn’t change Minimal code changes Integration withHibernate/Red H...
Not so good things! Dependency ofHibernate. Not compatible withHibernate-XML-Age. Extra table for eachtable.
Story Time!
References/Links www.jboss.org/envers http://docs.jboss.org/envers/docs/ http://www.warski.org/blog/
Thanks! 
Envers + Seam + JBoss: Why to use?
Upcoming SlideShare
Loading in …5
×

Envers + Seam + JBoss: Why to use?

1,259 views
1,049 views

Published on

My presentation on JUDCon Brazil 2013 - Santo Amaro, SP

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,259
On SlideShare
0
From Embeds
0
Number of Embeds
75
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Envers + Seam + JBoss: Why to use?

  1. 1. Envers + Seam + JBoss: Whyto use?Diogo Souza
  2. 2. Envers + Seam + JBoss:Why to use?Better...
  3. 3. The handshake
  4. 4. Diogo SouzaSystems Analyst at Indra Companyhttp://3adiX.com.brhttp://diogosouza.com.br@diogo_souza_c
  5. 5. Ah, the Envers!Once upon a time, in 2009…
  6. 6. What they say about…“The Envers project aims to enableeasy auditing/versioning of persistentclasses […]”Ref: http://www.jboss.org/envers
  7. 7. What they say about…“[…] All that you have to do isannotate your persistent class orsome of its properties, that you wantto audit, with @Audited. […]”Ref: http://www.jboss.org/envers
  8. 8. What they say about…“[…] For each audited entity, a tablewill be created, which will hold thehistory of changes made to theentity. […]”Ref: http://www.jboss.org/envers
  9. 9. And the best part…“[…] You can then retrieve and queryhistorical data without much effort.”Ref: http://www.jboss.org/envershttp://www.deviantart.com
  10. 10. Some features: Auditing of all mappings defined bythe JPA specification Auditing of some Hibernatemappings, which extend JPA Logging data for each revision usinga "revision entity" Querying historical dataRef: http://www.jboss.org/envershttp://www.deviantart.com
  11. 11. BasicallyEach transaction......One revisionINSERTUPDATEDELETE
  12. 12. Where to use?Wherever you have Hibernate! Standalone JBoss AS Spring
  13. 13. And how do we use in the past?<property name="hibernate.ejb.event.post-insert"value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" /><property name="hibernate.ejb.event.post-update"value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" /><property name="hibernate.ejb.event.post-delete"value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.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
  14. 14. 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
  15. 15. Simple sample@Entitypublic class Adress {@Id@GeneratedValueprivate int id;@Auditedprivate String streetName;@Auditedprivate Long zipCode;@Audited@ManyToOneprivate State state;}
  16. 16. The whole entity@Auditedpublic class Adress {@Id@GeneratedValueprivate int id;private String streetName;private Long zipCode;@ManyToOneprivate State state;}@Audited // Here too!public class State {@Id@GeneratedValueprivate int id;private String stateName;private Long population;}
  17. 17. … without a field@Auditedpublic class Adress {@Id@GeneratedValueprivate int id;@NotAuditedprivate String streetName;private Long zipCode;@ManyToOneprivate State state;}
  18. 18. Changing the entity tablename@Audited@AuditTable(“TB_ADRESS_AUDIT”)public class Adress {@Id@GeneratedValueprivate int id;@NotAuditedprivate String streetName;private Long zipCode;@ManyToOneprivate State state;}
  19. 19. The database0 ADD1 MOD3 DELThe revisiondate!
  20. 20. Querying for entities of aclass at a given revisionAuditQuery query =getAuditReader().createQuery().forEntitiesAtRevision(MyEntity.class,revisionNumber);
  21. 21. Querying for revisions, atwhich entities of a given classchangedAuditQuery query =getAuditReader().createQuery().forRevisionsOfEntity(MyEntity.class,false, true);
  22. 22. Querying for revisionsbetween revision datesAuditReader auditReader =AuditReaderFactory.get(entityManager);AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(Punishment.class, false, true).add(AuditEntity.revisionProperty("timestamp").gt(timeStamp).lt(otherTimeStamp).add(AuditEntity.revisionType().eq(RevisionType.ADD));List<Object[]> revisions = query.getResultList();
  23. 23. Configuration Properties// Prefixes and suffixesorg.hibernate.envers.audit_table_prefixorg.hibernate.envers.audit_table_suffix// Revision number and revision type fieldsorg.hibernate.envers.revision_field_nameorg.hibernate.envers.revision_type_field_name// Default schema usedorg.hibernate.envers.default_schema
  24. 24. Demo Time!
  25. 25. I am not responsible forany increases in thenumber of Brazilianpunishments...
  26. 26. The guy!Adam WarskiRef: http://www.warski.org/blog/about/http://warski.org
  27. 27. Good things! Easy! Data as always! Database schemadoesn’t change Minimal code changes Integration withHibernate/Red Hatrelated
  28. 28. Not so good things! Dependency ofHibernate. Not compatible withHibernate-XML-Age. Extra table for eachtable.
  29. 29. Story Time!
  30. 30. References/Links www.jboss.org/envers http://docs.jboss.org/envers/docs/ http://www.warski.org/blog/
  31. 31. Thanks! 

×