Hibernate的回调与拦截机制有三种实现方法:
1、实体对象implements Lifecycle接口,Lifecycle接口代码:
<!---->public interface Lifecycle {
/**
* 在实体对象Save/Insert操作之前触发.
*/
public boolean onSave(Session s) throws CallbackException;
/**
* 在Session.update()操作之前触发.
*/
public boolean onUpdate(Session s) throws CallbackException;
/**
* 在实体对象删除之前触发.
*/
public boolean onDelete(Session s) throws CallbackException;
/**<!---->
* 在实体对象加载之后触发.
*/
public void onLoad(Session s, Serializable id);
}
实体对象通过实现Lifecycle接口,即可以在特定的持久化阶段,触发特定的处理过程。比如在实体对象Tuser实现了Lifecycle接口的onSave方法,则在实体对象Tuser保存之前将先执行onSave方法。
2、实体对象implements Validatable接口,Validatable接口代码:
<!---->public interface Validatable {
public void validate() throws ValidationFailure;
}
Validatable接口定义了数据验证实现方式。实体对象实现Validatable接口,并在validate方法中对当前
待保存的数据进行验证,以保证数据的逻辑合法性(由于该方法在实体对象生命周期内,可能被多次调用,所以此方法最好只用于数据本身的逻辑合法性验证,而不要试图去实现数据业务逻辑的验证)。
以上2种方法都要求实现Hibernate中的Lifecycle或Validatable接口,具有很大的侵入性,使得实体对象的移植很不方便。Hibernate又提供了一种拦截机制,为对象持久化事件的捕获和处理提供了一个非侵入性的实现。
3、实现Interceptor接口,在创建session时,指定加载Interceptor相应的实现类,此session 的持久化操作都将首先经由此拦截器捕获处理。Interceptor(Hibernate3)接口代码:
<!---->package org.hibernate;
import java.io.Serializable;
import java.util.Iterator;
import org.hibernate.type.Type;
public interface Interceptor {
//对象初始化之前加载,这里的entity处于刚被创建的状态(即属性均未赋值).
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames,
Type[] types) throws CallbackException;
//Session.flush()方法进行脏数据检查时,如果发现PO状态改变,则调用此方法(即实体对象更新之前调用).
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException;
//在实体对象被保存之前调用.
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames,
Type[] types) throws CallbackException;
//在对象被删除之前调用.
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames,
Type[] types) throws CallbackException;
/**
* Called before a collection is (re)created.
*/
public void onCollectionRecreate(Object collection, Serializable key) throws CallbackException;
/**
* Called before a collection is deleted.
*/
public void onCollectionRemove(Object collection, Serializable key) throws CallbackException;
/**
* Called before a collection is updated.
*/
public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException;
//Session执行flush方法之前调用.
public void preFlush(Iterator entities) throws CallbackException;
//Session执行flush方法之后调用.
public void postFlush(Iterator entities) throws CallbackException;
/**
* Called to distinguish between transient and detached entities. The return value determines the
* state of the entity with respect to the current session.
* <ul>
* <li><tt>Boolean.TRUE</tt> - the entity is transient
* <li><tt>Boolean.FALSE</tt> - the entity is detached
* <li><tt>null</tt> - Hibernate uses the <tt>unsaved-value</tt> mapping and other heuristics to
* determine if the object is unsaved
* </ul>
* @param entity a transient or detached entity
* @return Boolean or <tt>null</tt> to choose default behaviour
*/
public Boolean isTransient(Object entity);
/**
* Called from <tt>flush()</tt>. The return value determines whether the entity is updated
* <ul>
* <li>an array of property indices - the entity is dirty
* <li>an empty array - the entity is not dirty
* <li><tt>null</tt> - use Hibernate's default dirty-checking algorithm
* </ul>
* @param entity a persistent entity
* @return array of dirty property indices or <tt>null</tt> to choose default behaviour
*/
public int[] findDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] types);
/**
* Instantiate the entity class. Return <tt>null</tt> to indicate that Hibernate should use
* the default constructor of the class. The identifier property of the returned instance
* should be initialized with the given identifier.
*
* @param entityName the name of the entity
* @param entityMode The type of entity instance to be returned.
* @param id the identifier of the new instance
* @return an instance of the class, or <tt>null</tt> to choose default behaviour
*/
public Object instantiate(String entityName, EntityMode entityMode,
Serializable id) throws CallbackException;
/**
* Get the entity name for a persistent or transient instance
* @param object an entity instance
* @return the name of the entity
*/
public String getEntityName(Object object) throws CallbackException;
/**
* Get a fully loaded entity instance that is cached externally
* @param entityName the name of the entity
* @param id the instance identifier
* @return a fully initialized entity
* @throws CallbackException
*/
public Object getEntity(String entityName, Serializable id) throws CallbackException;
/**
* Called when a Hibernate transaction is begun via the Hibernate <tt>Transaction</tt>
* API. Will not be called if transactions are being controlled via some other
* mechanism (CMT, for example).
*/
public void afterTransactionBegin(Transaction tx);
/**
* Called before a transaction is committed (but not before rollback).
*/
public void beforeTransactionCompletion(Transaction tx);
/** * Called after a transaction is committed or rolled back.
*/
public void afterTransactionCompletion(Transaction tx);
/**
* Called when sql string is being prepared.
* @param sql sql to be prepared
* @return original or modified sql
*/
public String onPrepareStatement(String sql);
}
分享到:
相关推荐
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
5.2 Hibernate回调与拦截机制 5.3 Hibernate实用技术 第6章 Hibernate 实战——创建RedSaga论坛 6.1 目标 6.2 E-R建模还是对象建模 6.3 E-R建模及工具集 6.4 对象建模 6.5 项目的目录组织和基础设施 6.6 测试...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了 Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
5.2 Hibernate回调与拦截机制 5.3 Hibernate实用技术 第6章 Hibernate 实战——创建RedSaga论坛 6.1 目标 6.2 E-R建模还是对象建模 6.3 E-R建模及工具集 6.4 对象建模 6.5 项目的目录组织和基础设施 6.6 测试...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了 Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用...
基于Hibernate持久层开发,Hibernate的回调和拦截机制
12.3.2 拦截Hibernate事件 12.3.3 内核事件系统 12.3.4 实体监听器和回调 12.4 小结 第13章 优化抓取和高速缓存 13.1 定义全局抓取计划 13.1.1 对象获取选项 13.1.2 延迟的默认抓取...
持久化生命周期(Lifecycle)中的回调(Callbacks) 4.5. 合法性检查(Validatable)回调 4.6. XDoclet标记示例 5. O/R Mapping基础 5.1. 映射声明(Mapping declaration) 5.1.1. Doctype 5.1.2. ...
1、拦截器基于动态代理 , 过滤器基于函数回调 2、拦截器不依赖于servlet容器,通过动态代理实现,过滤器依赖于servlet容器 3、拦截器在方法前后,异常前后等调用,而过滤器只能在请求前和请求后各调一次。 4、拦截...
持久化生命周期(Lifecycle)中的回调(Callbacks) 4.5. 合法性检查(Validatable)回调 4.6. XDoclet标记示例 5. O/R Mapping基础 5.1. 映射声明(Mapping declaration) 5.1.1. Doctype 5.1.2. ...