一般我们创建实体对象,属性基本都会有一个创建时间createDate和一个修改时间modifyDate,这两个属性在实际应用中也有着很重要的作用,通常,我们在保存实体的时候,需要对这两个属性都赋值,而且都是new Date()的方式赋值,实体多了,这种赋值就显得不是那么的方便了,我们可以考虑使用hibernate拦截器来帮助我们给他们赋值,当我们创建实体的时候,我们对createDate和modifyDate都要赋值,当我们修改实体的时候,我们就只对modifyDate进行赋值。

我们实现这个interceptor需要继承EmptyInterceptor,然后覆盖onSave(),onFlushDirty()两个方法。如下所示:

package com.xxx.spring.hibernate.config;
import java.io.Serializable;
import java.util.Date;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
@SuppressWarnings("serial")
public class MyEntityInterceptor extends EmptyInterceptor{
    private static final String CREATEDATE="createDate";
    private static final String MODIFYDATE="modifyDate";
    
    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, 
        String[] propertyNames, Type[] types) {
        for(int i=0;i<propertyNames.length;i++) {
            if(CREATEDATE.equals(propertyNames[i]) || 
               MODIFYDATE.equals(propertyNames[i])) {
                state[i] = new Date();
            }
        }
        return true;
    }
    
    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, 
            Object[] previousState,String[] propertyNames, Type[] types) {
        for(int i=0;i<propertyNames.length;i++) {
            if(MODIFYDATE.equals(propertyNames[i])) {
                currentState[i] = new Date();
            }
        }
        return true;
    }
}

在onSave()方法中,我们对createDate,modifyDate属性均赋值,在onFlushDirty()方法中,我们只对modifyDate赋值。

有了拦截器,我们就需要在配置文件中将他配置好(作为bean实体),并给sessionFactory属性entityInterceptor赋值。

<bean id="myEntityInterceptor" class="com.xxx.spring.hibernate.config.MyEntityInterceptor"/>
 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="entityInterceptor" ref="myEntityInterceptor"></property>
    <property name="hibernateProperties">
         <value>
             hibernate.dialect=${hibernate.dialect}
             hibernate.show_sql=${hibernate.show_sql}
             hibernate.format_sql=${hibernate.format_sql}
             hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
             hibernate.cache.use_second_level_cache=false
             hibernate.cache.use_query_cache=false
             hibernate.jdbc.fetch_size=50
             hibernate.jdbc.batch_size=50
             hibernate.connection.release_mode=auto
             hibernate.connection.autocommit=true
             hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
         </value>
    </property>
    <property name="packagesToScan" value="com.xxx.spring.hibernate.entity"></property>
 </bean>

这样,我们就实现并配置了这个拦截器,测试一下。

单元测试的时候,我们不手动赋值createDate,modifyDate属性。

hibernate配置entityInterceptor拦截器实现时间属性的赋值

执行单元测试之后,数据库中的数据,有了createdate,modifydate值:

hibernate配置entityInterceptor拦截器实现时间属性的赋值

相关文章:

  • 2021-04-18
  • 2021-08-09
  • 2021-09-14
  • 2021-07-16
  • 2021-04-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-03
  • 2022-01-03
  • 2022-12-23
  • 2021-07-08
  • 2021-11-18
相关资源
相似解决方案