Hibernate中的Interceptor 接口

 

这几天发现Hibernate中的Interceptor 的作用.使我很受感动.现在把他介绍给大家,希望对大家有帮助.也好相互学习

你可以在开启Session时载入一个自定义的Interceptor这个Interceptor会在对应的操作发生之前调用对应的方法,方法是你定义的实现了Interceptor接口的类的方法.Interceptor接口定义如下:

java 代码
  1. package org.hibernate;          
  2.          
  3. import java.io.Serializable;          
  4. import java.util.Iterator;          
  5.          
  6. import org.hibernate.type.Type;          
  7.          
  8. public interface Interceptor {          
  9.     // 载入对象前执行          
  10.     public      boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)           
  11.       throws CallbackException;          
  12.          
  13.     // flush 时,如果发现有Dirty data,则执行此方法          
  14.     public      boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,        String[] propertyNames, Type[] types) throws CallbackException;          
  15.          
  16.     // 保存对名象前执行          
  17.     public       boolean onSave(Object entity, Serializable id, Object[] state, String[]propertyNames, Type[] types)          throws CallbackException;          
  18.          
  19.     // 刪除对象前执行          
  20.     public    void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)    throws CallbackException;          
  21.          
  22.     // 在 flush 前执行          
  23.     public void preFlush(Iterator entities) throws CallbackException;          
  24.          
  25.     // 在 flush 后执行          
  26.     public void postFlush(Iterator entities) throws CallbackException;          
  27.            
  28.     // 判断传入的对象是否为 transient 状态          
  29.     public Boolean isTransient(Object entity);          
  30.          
  31.     // flush 前呼叫这个法判断 Dirty data          
  32.     // 传回Dirty data属性索引或null          
  33.     public      int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,    String[] propertyNames, Type[] types);          
  34.          
  35.     // 手动创建对象实体,如果传回 null,則使用预定的建造方法建立实例例          
  36.     public Object instantiate(String entityName, EntityMode entityMode, Serializable id)           
  37.       throws CallbackException;          
  38.          
  39.     // 传回对象名称     
  40.   public String getEntityName(Object object) throws CallbackException;          
  41.          
  42.     // 取得实例化对象          
  43.     public Object getEntity(String entityName, Serializable id) throws CallbackException;          
  44.          
  45.     // beginTransaction() 之后执行          
  46.     public void afterTransactionBegin(Transaction tx);          
  47.          
  48.     // 在事务完成前执行          
  49.     public void beforeTransactionCompletion(Transaction tx);          
  50.          
  51.     // 在事务完成后执行          
  52.     public void afterTransactionCompletion(Transaction tx);          
  53. }        

咱们自定一个MyInterceptor类:

java 代码
  1. package com.super;   
  2. ....   
  3. public class MyInterceptor implements Interceptor {   
  4.     ....   
  5. }  

 在启用Session时可以载入自订的Interceptor:

  1. MyInterceptor myInterceptor = new MyInterceptor();   
  2. Session session = sessionFactory.openSession(myInterceptor);   
  3. ....  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值