use of org.jpwh.model.filtering.interceptor.Auditable 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();
}
}
Aggregations