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

一般我们创建实体对象,属性基本都会有一个创建时间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属性。

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值