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);
}
posted on 2006-08-16 17:41
想飞的鱼 阅读(1203)
评论(0) 编辑 收藏 所属分类:
hibernate