use of org.jpwh.model.filtering.interceptor.AuditLogRecord in project microservices by pwillhan.
the class AuditLogInterceptor method postFlush.
/*
This method is called after flushing of the persistence context is complete.
Here, you write the audit log records for all insertions and updates you
collected earlier.
*/
public void postFlush(Iterator iterator) throws CallbackException {
/*
You are not allowed to access the original persistence context, the
<code>Session</code> that is currently executing this interceptor.
The <code>Session</code> is in a fragile state during interceptor calls.
Hibernate allows you to create a new <code>Session</code> that
inherits some information from the original <code>Session</code> with
the <code>sessionWithOptions()</code> method. Here the new temporary
<code>Session</code> works with the same transaction and database
connection as the original <code>Session</code>.
*/
Session tempSession = currentSession.sessionWithOptions().transactionContext().connection().openSession();
try {
/*
You store a new <code>AuditLogRecord</code> for each insertion and
update using the temporary <code>Session</code>.
*/
for (Auditable entity : inserts) {
tempSession.persist(new AuditLogRecord("insert", entity, currentUserId));
}
for (Auditable entity : updates) {
tempSession.persist(new AuditLogRecord("update", entity, currentUserId));
}
/*
You flush and close the temporary <code>Session</code>
independently from the original <code>Session</code>.
*/
tempSession.flush();
} finally {
tempSession.close();
inserts.clear();
updates.clear();
}
}
use of org.jpwh.model.filtering.interceptor.AuditLogRecord in project microservices by pwillhan.
the class AuditLogging method writeAuditLog.
@Test
public void writeAuditLog() throws Throwable {
UserTransaction tx = TM.getUserTransaction();
try {
Long CURRENT_USER_ID;
{
tx.begin();
EntityManager em = JPA.createEntityManager();
User currentUser = new User("johndoe");
em.persist(currentUser);
tx.commit();
em.close();
CURRENT_USER_ID = currentUser.getId();
}
EntityManagerFactory emf = JPA.getEntityManagerFactory();
Map<String, String> properties = new HashMap<String, String>();
properties.put(org.hibernate.jpa.AvailableSettings.SESSION_INTERCEPTOR, AuditLogInterceptor.class.getName());
EntityManager em = emf.createEntityManager(properties);
Session session = em.unwrap(Session.class);
AuditLogInterceptor interceptor = (AuditLogInterceptor) ((SessionImplementor) session).getInterceptor();
interceptor.setCurrentSession(session);
interceptor.setCurrentUserId(CURRENT_USER_ID);
tx.begin();
em.joinTransaction();
Item item = new Item("Foo");
em.persist(item);
tx.commit();
em.clear();
tx.begin();
em.joinTransaction();
List<AuditLogRecord> logs = em.createQuery("select lr from AuditLogRecord lr", AuditLogRecord.class).getResultList();
assertEquals(logs.size(), 1);
assertEquals(logs.get(0).getMessage(), "insert");
assertEquals(logs.get(0).getEntityClass(), Item.class);
assertEquals(logs.get(0).getEntityId(), item.getId());
assertEquals(logs.get(0).getUserId(), CURRENT_USER_ID);
em.createQuery("delete AuditLogRecord").executeUpdate();
tx.commit();
em.clear();
tx.begin();
em.joinTransaction();
item = em.find(Item.class, item.getId());
item.setName("Bar");
tx.commit();
em.clear();
tx.begin();
em.joinTransaction();
logs = em.createQuery("select lr from AuditLogRecord lr", AuditLogRecord.class).getResultList();
assertEquals(logs.size(), 1);
assertEquals(logs.get(0).getMessage(), "update");
assertEquals(logs.get(0).getEntityClass(), Item.class);
assertEquals(logs.get(0).getEntityId(), item.getId());
assertEquals(logs.get(0).getUserId(), CURRENT_USER_ID);
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
Aggregations