使用hibernate的拦截器(Interceptors)
package org.hibernate.test;
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.EmptyInterceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
public class AuditInterceptor extends EmptyInterceptor {
private int updates;
private int creates;
private int loads;
public void onDelete(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types) {
// do nothing
}
public boolean onFlushDirty(Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types) {
if ( entity instanceof Auditable ) {
updates++;
for ( int i=0; i < propertyNames.length; i++ ) {
if ( "这里放字段名".equals( propertyNames[i] ) ) {
........currentState[i]是属性值
return true;
}
}
}
return false;
}
public boolean onLoad(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types) {
if ( entity instanceof Auditable ) {
loads++;
}
return false;
}
public boolean onSave(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types) {
if ( entity instanceof Auditable ) {
creates++;
for ( int i=0; i<propertyNames.length; i++ ) {
if ( "这里放字段名".equals( propertyNames[i] ) ) {
........
return true;
}
}
}
return false;
}
public void afterTransactionCompletion(Transaction tx) {
if ( tx.wasCommitted() ) {
System.out.println("Creations: " + creates + ", Updates: " + updates, "Loads: " + loads);
}
updates=0;
creates=0;
loads=0;
}
}
在sping中配制
<bean id="auditInterceptor" class="org.hibernate.test.AuditInterceptor"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="entityInterceptor">
<ref bean="auditInterceptor"/>
</property>
</bean>
分享到:
相关推荐
spring mvc + spring + hibernate 全注解整合开发视频教程 11
以上是springmvc+spring+hibernate整合基于eclipse,oracle开发
spring mvc + spring + hibernate 全注解整合开发视频教程 06.haozip03
简单struts+spring+hibernate搭建,配置,适合初学者
gwt+spring+hibernate整合例子,导入myeclipse中(要安gwt插件)即可用,数据库自己去代码中查看。包是精简版的,去掉很多不必要的包
Spring+SpringMVC+Hibernate+maven项目,适用初学者学习。
spring mvc + spring + hibernate 全注解整合开发视频教程 04
spring mvc + spring + hibernate 全注解整合开发视频教程 12
Struts+Spring+Hibernate开发实例.pdf
DWR+Struts+spring+hibernate的订货系统,自己添加的dwr功能
基于struts+spring+hibernate+oracle的移动ssh项目源码 基于struts+spring+hibernate+oracle的移动ssh项目源码 基于struts+spring+hibernate+oracle的移动ssh项目源码 基于struts+spring+hibernate+oracle的移动ssh...
使用Struts + Spring + Hibernate完成用户登陆笔记使用Struts + Spring + Hibernate完成用户登陆笔记
农业网站 (ssh) struts 2 +spring+ hibernate农业网站 (ssh) struts 2 +spring+ hibernate农业网站 (ssh) struts 2 +spring+ hibernate农业网站 (ssh) struts 2 +spring+ hibernate农业网站 (ssh) struts ...
一个简单的spring+struts2+hibernate+mybatis整合(数据库脚本放在项目资源文件的sql目录下) 因为没想好mvc用springmvc好,还是struts2好 所以没有整合进去
Ajax+Spring+HibernateAjax+Spring+HibernateAjax+Spring+HibernateAjax+Spring+HibernateAjax+Spring+HibernateAjax+Spring+HibernateAjax+Spring+HibernateAjax+Spring+Hibernate
Spring+Hibernate+Strust 整合开发步骤 Spring+Hibernate+Strust 整合开发步骤 Spring+Hibernate+Strust 整合开发步骤
续: 博客系统 blog_1 因为文件较大无法一次性上传,故分为两步上传 blog_2里面是webroot(myeclipse编写) blog_1里面是源文件等 希望能给大家个帮助
Struts2+Spring+Hibernate+Jsp+Mysql5 项目申报系统.zip
eclipse工具,用Maven搭建的Spring+Spring MVC+Hibernate框架源码
spring+hibernate整合实现简单数据库添加记录,适合初学spring的学员