liveBook · Manning

Handler

As explained in Java Persistence with Spring Data and Hibernate epub

An event handler is a component that receives an event and performs processing in response to it.

An event handler receives an event and performs processing in response. In persistence applications, handlers add side effects or custom behavior when entities are created, saved, deleted, loaded, or linked. section 13.0 section 16.5.2

Persistence systems provide several event-handling approaches:

  • Use standard JPA lifecycle callbacks and event listeners for persist, load, and remove events. section 13.2
  • Use a Hibernate Interceptor when one component needs flexible access to persistence events. section 13.2 section 13.2.3
  • Use Hibernate’s core event SPI to extend or replace default listeners for lower-level engine behavior. section 13.2.3
  • Use declarative handlers for repository events, while listener classes extend an existing abstract hierarchy. section 16.5.2

Authorization during entity loading

A custom Hibernate load handler can inspect the entity class and identifier, reject unauthorized access, and then delegate authorized loading to the default implementation. Such handlers should avoid storing transaction-specific state because they are effectively shared across persistence contexts. section 13.2.3

Listing 13.3 Custom load event listener
Path: Ch13/interceptor/src/test/java/com/manning/javapersistence/ch13
 /filtering/SecurityLoadListener.java
 
public class SecurityLoadListener extends DefaultLoadEventListener {
    public void onLoad(LoadEvent event, LoadType loadType)
        throws HibernateException {
        boolean authorized =
            MySecurity.isAuthorized(
                event.getEntityClassName(), event.getEntityId()
            );
        if (!authorized) {
            throw new MySecurityException("Unauthorized access");
        }
        super.onLoad(event, loadType);
    }
}

Shows a concrete load-event handler that performs authorization before allowing Hibernate’s normal loading behavior to continue. section 13.2.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Compares JPA callbacks, Hibernate interceptors, core event listeners, and repository handlers, including configuration and singleton-like lifecycle constraints.
Examples
Uses authorization during entity loading, audit-log side effects, tenant assignment, and REST create, save, delete, and link events.
What you can do after
Apply it via JPA, Hibernate, or Spring Data REST extension points to execute custom logic around supported lifecycle events.
How it connects
Connects lifecycle callbacks, event listeners, interceptors, auditing, data filters, transactions, persistence contexts, and repository events.

Also explained in

Compare how 22 books explain handler →

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.