-
1. Re: Using Envers for Detecting Changes
lvdberg Jun 21, 2010 8:55 AM (in response to f7502)Hi,
maybe helpful for you. The following is a piece of code which detects changes between the status of (traffic) incidents. It reads the actual and previous version of the incident and provides to a specific part of a workflow where we look at different attributes.
It uses a unique code of the incident to retrieve it.... try { AuditReader reader = AuditReaderFactory.get(entityManager); AuditQuery aq = reader.createQuery().forRevisionsOfEntity( TrafficIncident.class, true, false); List list = aq.addOrder(AuditEntity.revisionNumber().desc()) .add(AuditEntity.property("uniqueCode").eq(code)) .setMaxResults(2).getResultList(); if (list.size() == 1){ actualIncident = (TrafficIncident) list.get(0); previousIncident = null; } else if (list.size() == 2){ actualIncident = (TrafficIncident) list.get(0); previousIncident = (TrafficIncident) list.get(1); } } catch (WhateverException e) { log.error("AUDIT ACCESS DOESN'T WORK!! IF YOU GET HERE", e); }
You need to look up the rest in the envers doc, which is not great but sufficient. Try to find the Seam7Envers example. Which let you put additional context data in the audit tables.
Leo
-
2. Re: Using Envers for Detecting Changes
f7502 Jun 21, 2010 9:18 AM (in response to f7502)Hi Leo,
yes, i think this will be helpful. Thanks!
There remains one fundamental question concerning your code example:
Where / How do i get the entityManager? -
3. Re: Using Envers for Detecting Changes
lvdberg Jun 21, 2010 9:35 AM (in response to f7502)Hi,
Just inject it in your bean e.g.
@In EntityManager entityManager;
... or whatever name you have given to the entity manager in components.xml
Leo
-
4. Re: Using Envers for Detecting Changes
f7502 Jun 21, 2010 10:31 AM (in response to f7502)ok, got it
in my project, the EntityManager is registered in JNDI, so i got to get it from there. It seems to work now, thanks for your help!