(6)Hibernate 中的监听器和拦截器

Hibernate 就总结这么多,本来还有集合过滤,过滤器这一块的,但这次的例子只有一张表就使用。

 

针对 SaveOrUpate 的监听器 (针对 Save 没有解决)

监听器com.isw2.Bean.MySaveListener.java

/**
 * 继承DefaultSaveOrUpdateEventListener 实现监听器
 * 
 * @author Administrator
 * 
 */
public class MySaveListener extends DefaultSaveOrUpdateEventListener {

	@Override
	public void onSaveOrUpdate(SaveOrUpdateEvent arg0) {
		System.out.println("MySaveListener.onSaveOrUpdate \n\t"
				+ arg0.getObject().toString());
		super.onSaveOrUpdate(arg0);
	}

}

 监听器注册方式1 在hibernate.cfg.xml 中:

<!-- 注册监听器 -->
		<!-- 
			<event type="save-update">
			<listener class="com.isw2.Bean.MySaveListener" />
			</event>
                -->

 监听器注册方式2(类HibernateSessionFactory.java 中 Eclipse 自动生成)

/**
 * 在 SessionFactory 中使用 MySaveListener 监听器
 * 
 * @param interceptor
 * @return
 * @throws HibernateException
 */
public static Session getSessionFactoryListener(Object object)
		throws HibernateException {
	Session session = (Session) threadLocal.get();

	if (session == null || !session.isOpen()) {
		// 设置MySaveListener 监听器
		configuration.setListener("save-update", object);
		sessionFactory = configuration.buildSessionFactory();
		session = (sessionFactory != null) ? sessionFactory
				.openSession() : null;
		threadLocal.set(session);
	}

	return session;
}

 

 

 

拦截器的实现:

com.isw2.Bean.TestInterceptor.java

/**
 * 拦截器 继承类 EmptyInterceptor 重写里面的方法
 * 
 * @author Administrator
 * 
 */
public class TestInterceptor extends EmptyInterceptor {

	@Override
	public boolean onLoad(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {
		System.out.println("TestInterceptor.onLoad......");
		return super.onLoad(entity, id, state, propertyNames, types);
	}

	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state,
			String[] propertyNames, Type[] types) {
		System.out.println("TestInterceptor.onSave........");
		return super.onSave(entity, id, state, propertyNames, types);
	}

}

 使用拦截器(sessionFactory 中和 Session 中):

/**
 * 在 SessionFactory 中使用 Interceptory 拦截器
 * 
 * @param interceptor
 * @return
 * @throws HibernateException
 */
public static Session getSessionFactoryInterceptor(Interceptor interceptor)
		throws HibernateException {
	Session session = (Session) threadLocal.get();

	if (session == null || !session.isOpen()) {
		// 设置Interceptor 拦截器
		configuration.setInterceptor(interceptor);
		sessionFactory = configuration.buildSessionFactory();
		session = (sessionFactory != null) ? sessionFactory
				.openSession(interceptor) : null;
		threadLocal.set(session);
	}

	return session;
}

/**
 * Session 中启用Interceptor
 * 
 * @param interceptor
 * @return
 * @throws HibernateException
 */
public static Session getSessionInterceptor(Interceptor interceptor)
		throws HibernateException {
	Session session = (Session) threadLocal.get();

	if (session == null || !session.isOpen()) {
		if (sessionFactory == null) {
			rebuildSessionFactory();
		}
		session = (sessionFactory != null) ? sessionFactory
				.openSession(interceptor) : null;
		threadLocal.set(session);
	}

	return session;
}

 关于过滤器和集合过滤器可以参考:

http://42087743.iteye.com/blog/305219  

http://blog.csdn.net/javacoffe/archive/2007/08/09/1733579.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值