I am using Hiberante 5 and Spring 4.2.3. I don't find the way to add an eventListener to SessionFactory scope. I just need to set a date before hibernate persists an object. I have sessionFactory defined in spring.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/hibernate/mapping/User.hbm.xml</value>
<value>com/hibernate/mapping/PublicKey.hbm.xml</value>
<value>com/hibernate/mapping/File.hbm.xml</value>
<value>com/hibernate/mapping/Url.hbm.xml</value>
<value>com/hibernate/mapping/Role.hbm.xml</value>
<value>com/hibernate/mapping/Operation.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">50</prop>
<prop key="hibernate.c3p0.timeout">100</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
<prop key="hibernate.c3p0.validate">true</prop>
</props>
</property>
</bean>
And I have my GenericDAOImpl where a get this sessionFactory:
public abstract class GenericDAOImpl <T, PK extends Serializable> implements GenericDAO<T, PK> {
private SessionFactory sessionFactory;
/** Domain class the DAO instance will be responsible for */
protected Class<T> type;
@SuppressWarnings("unchecked")
public GenericDAOImpl() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
type = (Class<T>) pt.getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
public PK create(T o) {
return (PK) getSession().save(o);
}
public T read(PK id) {
return (T) getSession().get(type, id);
}
public void update(T o) {
getSession().update(o);
}
public void delete(T o) {
getSession().delete(o);
}
@SuppressWarnings("unchecked")
public List<T> getAll() {
return getSession().createCriteria(type).list();
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
// getters and setters
}
I have seen several ways to do it, but some of them doesn't work for Hiberante 5 (Like an Integrator
). And I can't find the way to add this an eventListener via xml or just adding in the code in the sessionFactory scope. I have found how to add to session scope with sessionFactory.withOptions().eventListeners(listeners);
Thanks!
Finally, it was so easy, I found a way to add via xml an interceptor, just adding:
<property name="entityInterceptor">
<bean class="com.xxx.xxx.className"></bean>
</property>
I found this in the post: Hibernate Interceptor Not Working
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With