Java Reference
In-Depth Information
Transaction tx = session.beginTransaction();
// Make our bookings...
session.save(new Booking("dave","F1"));
session.save(new Booking("jeff","C3"));
// The confirmation letters should not be sent
// out until AFTER the commit completes.
tx.commit();
}
}
The interceptor that we are applying is going to capture the information from the Booking
objects that we are storing in the database. Listing A-26 demonstrates the basic mechanism,
but it is only a toy example. We will discuss some of its deficiencies in a moment.
Listing A-26. An Interceptor Implementation
package com.hibernatebook.advanced.events;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
public class BookingInterceptor implements Interceptor {
public BookingInterceptor() {
}
private ThreadLocal stored = new ThreadLocal();
public void afterTransactionBegin(Transaction tx) {
stored.set(new HashSet());
}
public void afterTransactionCompletion(Transaction tx) {
if (tx.wasCommitted()) {
Iterator i = ((Collection) stored.get()).iterator();
Search WWH ::




Custom Search