org.hibernate.Interceptor is Hibernate’s proprietary contract for reacting to persistence operations and adding application-defined behavior. It is useful when entity changes must trigger cross-cutting logic, such as recording who created or updated an auditable entity. Compared with JPA lifecycle callbacks, it provides a more fine-grained alternative, while Hibernate’s core event system offers still lower-level extension points. section 13.2 section 13.2.1
An audit interceptor typically follows this implementation flow:
- Mark target entities with an Auditable interface exposing an identifier, such as getId(). section 13.2.2
- Extend EmptyInterceptor and override only the needed callbacks, collecting auditable entities in onSave() and update handling. section 13.2.2
- Write audit records in postFlush(), after Hibernate has assigned identifiers and the modified entity state has been collected. section 13.2.2
Application-side audit logging
An AuditLogInterceptor can collect Item instances implementing Auditable during inserts and updates, then open a temporary Session in postFlush() to persist AuditLogRecord entries. This keeps audit logging in application code rather than database triggers, which can help when database portability is required. section 13.2.2
