Is there a way to use the Hibernate event architecture to fire an event when an object is fetched using a query, the same way a LoadEvent is fired when an object is loaded?
I am using hibernate 3.2.5.ga and hibernate-annotations 3.3.0.ga with Spring 2.0.2. I have this in my sessionFactory definition:
Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
<property name="eventListeners">
<map>
<entry>
<key><value>load</value></key>
<list>
<ref bean="defaultHibernateLoadEventListener" />
<ref bean="serviceInjectingLoadEventListener" />
</list>
</entry>
</map>
</property>
</bean>
And the serviceInjectingLoadEventListener executes as expected every time I do a Session.load:
Code:
public SomeEntity loadSomeEntity(Serializable id) {
Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
return (SomeEntity) session.load(SomeEntity.class, id);
}
But I would also like to execute the same listener during a query, like this:
Code:
public SomeEntity findSomeEntityBySomeProperty(String someProperty) {
Session session = SessionFactoryUtils.getSession(this.getSessionFactory(), true);
Criteria crit = session.createCriteria(SomeEntity.class);
crit.add(Restrictions.eq("someProperty", someProperty));
List list = crit.list();
return (SomeEntity) CollectionUtils.findOne(list);
}
Is there a good way to do this?