title: How do I set up an event listener on a Hibernate 4 entity?
body: <p>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 </p>
  
  <pre><code>@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&lt;EventFeed&gt; results = crit.list();
          if (results != null &amp;&amp; results.size() &gt; 0) { 
              eventFeed = results.get(0);
          }   // if
          return eventFeed;       
      }   // findById 
  </code></pre>
  
  <p>Here is how I'm trying to set up my event wiring ...</p>
  
  <pre><code>@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);
      }
  }
  </code></pre>
  
  <p>and my listener class is as follows ...</p>
  
  <pre><code>@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
      }
  
  }
  </code></pre>
  
  <p>but the "onLoad" event is never called.  What am I doing wrong?  Should I be using another event?</p>