hibernate拦截器

在后端逻辑开发过程中,必然有数据的字段是重复的,是需要保存的,比如当前用户修改了订单,需要记录修改人,修改时间等信息至数据库中,此时没有引用hibernate拦截器时,是需要在每个方法里面都添加对应的代码,记录修改人,修改时间等等字段信息,对这些公用信息每个修改操作都得重复复制,比如在大型电子商务网站中,就必然涉及到很多操作,表结构成百上千,就得在成百上千的后台代码都复制同样的代码进行赋值。

先不说工作量多少,没多少工作量,就是复制下就可。至少这个会多很多不必要的代码,对于系统架构来说,这个就需要在一开始就处理好。此时正好hibernate拦截器就出现了。同时这个拦截器不会对系统造成任何影响,可以随时剔除,同时也不会对框架整体性造成错误影响,完全是个即插即用的。

package com.todaysteel.esaasfront.whs.common;


import java.io.Serializable;
import java.util.Date;

import org.apache.log4j.Logger;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;

import com.todaysteel.core.utils.security.SpringSecurityUtils;
import com.todaysteel.esaasfront.publiccloud.commom.security.vo.Authorization;

/**
 * @author liuhaixiao
 * hibernate拦截器:
 * 实现创建人,创建时间自动注入;
 * 修改人,修改时间自动注入;
 */
@Component
public class PropertyInterceptor extends EmptyInterceptor {

	private static final Logger log = Logger.getLogger(PropertyInterceptor.class);
	private static final long serialVersionUID = 1L;


	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state,
		String[] propertyNames, Type[] types) {
		log.info("------Hibernate Interceptor-----onSave-----");
		Authorization user = SpringSecurityUtils.getCurrentUser();
		//添加数据
		for (int index=0;index<propertyNames.length;index++) {
			//找到名为"添加日期"的属性
			if ("dadddate".equals(propertyNames[index])) {
	             //使用拦截器将对象的"添加日期"属性赋上值
	        	if(state[index]==null){
	        		state[index] = new Date();
	        	}
	            continue;
	        } else if ("saddoperator".equals(propertyNames[index])) {//找到名为"添加人"的属性
	        	//使用拦截器将对象的"添加人"属性赋上值
	        	if(state[index]==null){
	        		state[index] = user.getUsername();
	        	}
	            continue;
	        } 
		}
		return true;
	}


	@Override
	public boolean onFlushDirty(Object entity, Serializable id,
			Object[] currentState, Object[] previousState,
			String[] propertyNames, Type[] types) {
		log.info("------Hibernate Interceptor-----onFlushDirty-----");
		Authorization user = SpringSecurityUtils.getCurrentUser();
		//添加数据
	     for (int index=0;index<propertyNames.length;index++) {
	         /*找到名为"修改时间"的属性*/
	         if ("dmodifydate".equals(propertyNames[index])) {
	             /*使用拦截器将对象的"修改时间"属性赋上值*/
	        	 currentState[index] = new Date();
	             continue;
	         } else if ("smodifyoperator".equals(propertyNames[index])) {/*找到名为"修改人"的属性*/
	             /*使用拦截器将对象的"修改人"属性赋上值*/
	        	 currentState[index] = user.getUsername();
	        	 continue;
	         } 
	     }
	
		 return true;
	}

}

 hibernate提供了很多拦截器,在实际需要用到时才查看源码,类似于spring的AOP机制。

同理对电子商务网站需要监控日志的内容同样可以用此拦截器进行跟踪用户的行为。

当然最好是用AOP来处理跟踪用户的日志行为。

 

由于我是采用配置的方式来采用拦截器,所以需要在xml中进行配置。

<!-- 自定义hibernate属性拦截器 -->
	<bean id ="proInterceptor" class="com.todaysteel.esaasfront.whs.common.PropertyInterceptor" />

	<!-- Hibernate配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="DynamicDataSource" />
		<property name="namingStrategy">
			<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.todaysteel.esaasfront,com.todaysteel.core.business" />
		<property name="entityInterceptor" ref="proInterceptor"/>
	</bean>

 我把这个拦截器的作用域是放在session级别的,所以配置在sesisonFactory上。

 

 

 

 

 

 

### 整合 Hibernate 拦截器到 Spring Boot 的方法 在 Spring Boot 中整合 Hibernate 拦截器可以用于实现自定义逻辑或审计功能。以下是具体的方法: #### 配置 `LocalSessionFactoryBean` 或 `EntityManagerFactory` 由于 Spring Boot 自动配置了 Hibernate,因此可以通过设置 `hibernate.session_factory.interceptor` 属性来指定拦截器类。 ```properties spring.jpa.properties.hibernate.session_factory.interceptor=com.example.CustomInterceptor ``` 此属性会告诉 Hibernate 使用哪个拦截器实例[^1]。 #### 创建自定义拦截器类 创建一个继承自 `org.hibernate.EmptyInterceptor` 的类,并重写所需的方法。例如: ```java import org.hibernate.EmptyInterceptor; import java.io.Serializable; public class CustomInterceptor extends EmptyInterceptor { @Override public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { // 实现保存前的逻辑 System.out.println("Entity is being saved: " + entity); return super.onSave(entity, id, state, propertyNames, types); } @Override public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { // 实现在删除之前的逻辑 System.out.println("Entity is being deleted: " + entity); super.onDelete(entity, id, state, propertyNames, types); } } ``` 上述代码展示了如何通过覆盖 `onSave` 和 `onDelete` 方法,在实体保存和删除之前执行特定操作。 #### 注册拦截器到 Spring 应用上下文中 如果需要动态注册拦截器,则可以在应用启动时将其注入到 Bean 定义中。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, DataSource dataSource) { Map<String, Object> properties = new HashMap<>(); properties.put("hibernate.session_factory.interceptor", customInterceptor()); return builder.dataSource(dataSource).packages("com.example").persistenceUnit("unitName") .properties(properties).build(); } @Bean public CustomInterceptor customInterceptor() { return new CustomInterceptor(); } } ``` 这段代码显示了如何将拦截器作为 Bean 进行管理并传递给 `entityManagerFactory` 的配置过程[^2]。 #### 测试集成效果 完成以上步骤后,运行应用程序并通过调试验证拦截器是否按预期工作。每次触发数据库交互时,相应的拦截器方法会被调用。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值