liveBook · Manning

Org.hibernate.Interceptor

As explained in Java Persistence with Spring Data and Hibernate epub

org.hibernate.Interceptor is a Hibernate API contract for intercepting and reacting to persistence operations with application-defined behavior.

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

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains selective EmptyInterceptor overrides, flush-time identifier timing, Session configuration, and defers deeper behavior to core event listeners.
Examples
Uses Auditable Item entities, AuditLogRecord entities, current-user identifiers, and insert or update audit events.
What it assumes
Assumes familiarity with persistent entities, Hibernate Sessions, lifecycle events, and database writes.
What you can do after
Build it from scratch by extending EmptyInterceptor, collecting entity changes, and attaching the interceptor to a Hibernate Session.
How it connects
Compares JPA lifecycle callbacks, Hibernate event listeners, database triggers, and Hibernate Envers as related interception or auditing mechanisms.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.