【问题标题】:EventListener Hibernate 5事件监听器休眠 5
【发布时间】:2016-06-21 16:02:51
【问题描述】:

我正在使用 Hiberante 5 和 Spring 4.2.3。我找不到将 eventListener 添加到 SessionFactory 范围的方法。我只需要在 hibernate 持久化一个对象之前设置一个日期。我在 spring.xml 中定义了 sessionFactory

<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>

我有我的 GenericDAOImpl ,在那里可以得到这个 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
}

我已经看到了几种方法,但其中一些方法不适用于 Hiberante 5(如 Integrator)。而且我找不到通过xml添加这个eventListener的方法,或者只是在sessionFactory范围内添加代码。我找到了如何使用sessionFactory.withOptions().eventListeners(listeners);添加到会话范围

谢谢!

【问题讨论】:

  • @MarcoA.Hernandez 我在 eventListener 之前尝试过这种可能性,但我没有找到通过 xml 将拦截器添加到 SessionFactory 的方法。我知道使用代码就像new Configuration().setInterceptor( new AuditInterceptor() ) 一样简单。但我已经在 xml 中定义了我的 sessionFactory。
  • 从未尝试过,但我想你可以创建一个扩展 org.springframework.orm.hibernate5.LocalSessionFactoryBean 的类,在构造函数中添加 Configuration().setInterceptor(...) 然后在xml配置文件。
  • @MarcoA.Hernandez 我刚刚找到了解决方案。谢谢!

标签: java spring hibernate hibernate-5.x


【解决方案1】:

终于这么简单了,我找到了一种通过xml添加拦截器的方法,只需添加:

<property name="entityInterceptor">
    <bean class="com.xxx.xxx.className"></bean>
</property>

我在帖子中发现了这个: Hibernate Interceptor Not Working

【讨论】:

    猜你喜欢
    • 2012-01-26
    • 2018-12-04
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多