5

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!

angeldev
  • 1,983
  • 3
  • 18
  • 29
  • Have you tried with Interceptors? http://docs.jboss.org/hibernate/core/3.3/reference/en/html/events.html – Marco A. Hernandez Jun 21 '16 at 16:11
  • @MarcoA.Hernandez I was trying this possibility before eventListener but I didn't find the way to add the interceptor to SessionFactory via xml. I know that with code is as simple as `new Configuration().setInterceptor( new AuditInterceptor() )`. But I have defined my sessionFactory in xml. – angeldev Jun 21 '16 at 16:13
  • Never tried but I suppose you can create a class that extends org.springframework.orm.hibernate5.LocalSessionFactoryBean, add Configuration().setInterceptor(...) in the constructor and then use it in the xml config file. – Marco A. Hernandez Jun 21 '16 at 16:30
  • @MarcoA.Hernandez I have just found a solution. Thanks! – angeldev Jun 21 '16 at 16:30

1 Answers1

1

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

Community
  • 1
  • 1
angeldev
  • 1,983
  • 3
  • 18
  • 29