Dave
Dave

Reputation: 19340

How do I set up an event listener on a Hibernate 4 entity?

I'm using Spring 3.1.0.RELEASE with Hibernate 4.0.1.Final. I want to invoke an event listener on my entity bean when it is loaded from the DB, but I can't figure out what event I should be using. I load my entities in my DAO like so

@Repository("eventFeedsDao")
public class EventFeedsDaoImpl implements EventFeedsDao {

    ...
    @Autowired
    private SessionFactory sessionFactory;

    ...

    @Override
    public EventFeed findById(final Integer id) { 
        EventFeed eventFeed = null;
        final Session session = sessionFactory.getCurrentSession();
        final Criteria crit = session.createCriteria(EventFeed.class).add(Restrictions.eq("id", id));
        final List<EventFeed> results = crit.list();
        if (results != null && results.size() > 0) { 
            eventFeed = results.get(0);
        }   // if
        return eventFeed;       
    }   // findById 

Here is how I'm trying to set up my event wiring ...

@Component
public class HibernateEventWiring {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private EventMavenLoadListener listener;

    @PostConstruct
    public void registerListeners() {
        EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory)
            .getServiceRegistry().getService(EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.LOAD).appendListener(listener);
    }
}

and my listener class is as follows ...

@Component
public class EventMavenLoadListener implements LoadEventListener {

    ...

    @Override
    public void onLoad(final LoadEvent event, final LoadType loadType) throws HibernateException {
        if(EventFeed.class.getName().equals(event.getEntityClassName())){
            EventFeed entity = (EventFeed) event.getInstanceToLoad();
            entity.setNetUtilsService(netUtilsService);
        }   // if
    }

}

but the "onLoad" event is never called. What am I doing wrong? Should I be using another event?

Upvotes: 3

Views: 12505

Answers (2)

Steve Ebersole
Steve Ebersole

Reputation: 9443

The load event is only triggered when loading an entity individually (session.get, initializing a proxy, fetching eager to-one associations).

The pre and post load events however follow JPA defined behavior and occur before and after the entity data is loaded regardless of how that "data load" occurred.

Its a case of bad name collision.

The preferred way in Hibernate 4, btw, to specify event listeners is through the use of an org.hibernate.integrator.spi.Integrator. See http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html_single/#integrators

Upvotes: 0

Dave
Dave

Reputation: 19340

For some reason that still eludes me, the EventType.PRE_LOAD event type was the way to go. This worked.

@Component
public class HibernateEventWiring {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private EventMavenLoadListener listener;

    @PostConstruct
    public void registerListeners() {
        final EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory)
                .getServiceRegistry().getService(EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.PRE_LOAD)
                .appendListener((PreLoadEventListener) listener);
    }
}

Upvotes: 2

Related Questions