O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Envers + Seam + JBoss: Why to use?

1.466 visualizações

Publicada em

My presentation on JUDCon Brazil 2013 - Santo Amaro, SP

Publicada em: Tecnologia
  • Seja o primeiro a comentar

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! 

×