Hibernate拦截器不工作

Hibernate拦截器不工作

本文介绍了Hibernate拦截器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的HibernateInterceptor,其中基本上我想自动设置几个字段。这个拦截器(如下所示)扩展了EmptyInterceptor:

  public class EntityAuditInterceptor extends EmptyInterceptor {

/ **
*串行版本UUID。
* /
private static final long serialVersionUID = 4636626262147801287L;
$ b $ *(非Javadoc)
* @see org.hibernate.EmptyInterceptor#onFlushDirty(java.lang.Object,java.io.Serializable,java.lang.Object [], java.lang.Object [],java.lang.String [],org.hibernate.type.Type [])
* /
public boolean onFlushDirty(Object entity,Serializable id,Object [] currentState ,Object [] previousState,String [] propertyNames,Type [] types){

//在这里做东西

return false;

$ b $ *(非Javadoc)
* @see org.hibernate.EmptyInterceptor#onSave(java.lang.Object,java.io.Serializable,java.lang .Object [],java.lang.String [],org.hibernate.type.Type [])
* /
public boolean onSave(Object entity,Serializable id,Object [] state,String [ ] propertyNames,Type []类型){

//在这里做的事
return false;




$ b我使用弹簧配置文件进行接线,如下所示:

 <! -  Hibernate SessionFactory  - > 
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =hsqlDbDataSource/>

< property name =packagesToScan>
< list>
< value> com.dreamteam.lms。**。*< / value>
< / list>
< / property>

< property name =entityInterceptor>
< bean class =com.dreamteam.lms.interceptors.EntityAuditInterceptor>< / bean>
< / property>


< property name =hibernateProperties>
<道具>
<! - < prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop> - >
< prop key =hibernate.dialect> org.hibernate.dialect.HSQLDialect< / prop>
< prop key =hibernate.generate_statistics> true< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.cache.use_second_level_cache> true< / prop>
< prop key =hibernate.hbm2ddl.auto>建立< / prop>
< prop key =hibernate.cache.use_query_cache> true< / prop>
< prop key =hibernate.cache.provider_class> net.sf.ehcache.hibernate.SingletonEhCacheProvider< / prop>
< /道具>
< / property>
< / bean>

但是,拦截器永远不会到达。有没有人有任何线索?我也尝试在事务管理器bean定义中添加以下内容:

 < property name =entityInterceptor> 
< ref local =entityAuditInterceptor/>
< / property>


解决方案

好的,只是为了记录,我解决了这个问题结果这是我的一个愚蠢的错误。

当我实现了扩展EmptyInterceptor的拦截器时,我添加了方法'onFlushDirty'等等。到现在为止还挺好。问题在于,在使用我的IDE自动导入使用的类时,我最终导致错误地导入了java.reflect.Type而不是org.hibernate.type.Type。因此,我并不是真正覆盖拦截器方法!



我注意到当我将@Override拦截器添加到我的方法中时。



另一个谜题解决了......)

I have a simple HibernateInterceptor in which basically I want to set a few fields automatically. This interceptor (shown below) extends EmptyInterceptor:

public class EntityAuditInterceptor extends EmptyInterceptor {

    /**
     * The Serial Version UUID.
     */
    private static final long serialVersionUID = 4636626262147801287L;

    /* (non-Javadoc)
     * @see org.hibernate.EmptyInterceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
     */
    public boolean onFlushDirty(Object entity,  Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {

        // doing stuff here

        return false;
    }

    /* (non-Javadoc)
     * @see org.hibernate.EmptyInterceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
     */
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {

                // doing stuff here
        return false;
    }
}

I am wiring using a spring config file as follows:

<!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="hsqlDbDataSource"/>

        <property name="packagesToScan">
            <list>
                <value>com.dreamteam.lms.**.*</value>
            </list>
        </property>

        <!-- Adding Interceptor here -->
        <property name="entityInterceptor">
            <bean class="com.dreamteam.lms.interceptors.EntityAuditInterceptor"></bean>
        </property>


        <property name="hibernateProperties">
            <props>
                <!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>-->
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
            </props>
        </property>
    </bean>

However, the Interceptor is never reached. Does anyone have any clue? I have also tried adding the following to the transaction manager bean definition as follows:

<property name="entityInterceptor">
        <ref local="entityAuditInterceptor"/>
    </property>
解决方案

Ok, just for the record, I solved this problem which turned out to be a stupid mistake from my part.

When I implemented my interceptor that extends EmptyInterceptor I added the method 'onFlushDirty' and so on. So far so good. The problem was that on using my IDE to auto-import the used classes, I ended up importing java.reflect.Type instead of org.hibernate.type.Type by mistake. Hence, I was not really overriding the interceptor method!

I noticed that when I added the @Override interceptor to my method.

Another mystery solved ... :)

这篇关于Hibernate拦截器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:13