Java Reference
In-Depth Information
Listing A-23. The Implementation of an Event Listener
package com.hibernatebook.advanced.events;
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.def.DefaultSaveOrUpdateEventListener;
public class BookingSaveOrUpdateEventListener
extends DefaultSaveOrUpdateEventListener
{
public Serializable onSaveOrUpdate(SaveOrUpdateEvent event)
throws HibernateException {
if( event.getObject() instanceof Booking ) {
Booking booking = (Booking)event.getObject();
System.out.println("Preparing to book seat " + booking.getSeat());
if( booking.getSeat().equalsIgnoreCase("R1")) {
System.out.println("Royal box booked");
System.out.println("Conventional booking not recorded.");
// By returning null instead of invoking the
// default behavior‚ we prevent the invocation
// of saveOrUpdate on the Session from having
// any effect on the database!
return null;
}
}
// The default behavior:
return super.onSaveOrUpdate(event);
}
}
Interceptors
Interceptors are privy to a blow-by-blow account of what is going on as Hibernate carries out
its duties. While you can listen in, you can only make limited changes to the way in which
Hibernate actually behaves. This is the common requirement; unless you are making substan-
tial changes to the persistence behavior, you will usually want only to track what is going on.
Search WWH ::




Custom Search