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





















