【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“Cascades.java”一文,供大家参考学习
//$Id: Cascades.java,v 1.17.2.14 2003/11/29 10:22:22 oneovthafew Exp $
package net.sf.hibernate.engine;
import java.io.Serializable;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.LockMode;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.ReplicationMode;
import net.sf.hibernate.collection.CollectionPersister;
import net.sf.hibernate.collection.PersistentCollection;
import net.sf.hibernate.persister.ClassPersister;
import net.sf.hibernate.type.AbstractComponentType;
import net.sf.hibernate.type.AssociationType;
import net.sf.hibernate.type.PersistentCollectionType;
import net.sf.hibernate.type.Type;
/**
* Implements cascaded save / delete / update / lock / evict / replicate
*
* @see net.sf.hibernate.type.AssociationType
* @author Gavin King
*/
public final class Cascades {
private Cascades() {}
private static final Log log = LogFactory.getLog(Cascades.class);
// The available cascade actions:
/**
* A session action that may be cascaded from parent entity to its children
*/
public abstract static class CascadingAction {
protected CascadingAction() {}
/**
* cascade the action to the child object
*/
abstract void cascade(SessionImplementor session, Object child, Object anything) throws HibernateException;
/**
* Should this action be cascaded to the given (possibly uninitialized) collection?
*/
abstract Iterator getCascadableChildrenIterator(PersistentCollectionType collectionType, Object collection);
/**
* Do we need to handle orphan delete for this action?
*/
abstract boolean deleteOrphans();
}
/**
* @see net.sf.hibernate.Session#delete(Object)
*/
public static final CascadingAction ACTION_DELETE = new CascadingAction() {
void cascade(SessionImplementor session, Object child, Object anything) throws HibernateException {
log.trace("cascading to delete()");
if ( session.isSaved(child) ) session.delete(child);
}
Iterator getCascadableChildrenIterator(PersistentCollectionType collectionType, Object collection) {
// delete does cascade to uninitialized collections
return getAllElementsIterator(collectionType, collection);
}
boolean deleteOrphans() {
// orphans should be deleted during delete
return true;
}
};
/**
* @see net.sf.hibernate.Session#lock(Object, LockMode)
*/
public static final CascadingAction ACTION_LOCK = new CascadingAction() {
void cascade(SessionImplementor session, Object child, Object anything) throws HibernateException {
log.trace("cascading to lock()");
session.lock( child, (LockMode) anything );
}
Iterator getCascadableChildrenIterator(PersistentCollectionType collectionType, Object collection) {
// lock doesn't cascade to uninitialized collections
return getLoadedElementsIterator(collectionType, collection);
}
boolean deleteOrphans() {
//TODO: should orphans really be deleted during lock???
return false;
}
};
/**
* @see net.sf.hibernate.Session#evict(Object)
*/
public static final CascadingAction ACTION_EVICT = new CascadingAction() {
void cascade(SessionImplementor session, Object child, Object anything) throws HibernateException {
log.trace("cascading to evict()");
session.evict(child);
}
Iterator getCascadableChildrenIterator(PersistentCollectionType collectionType, Object collection) {
// evicts don't cascade to uninitialized collections
return getLoadedElementsIterator(collectionType, collection);
}
boolean deleteOrphans() {
return false;
}
};
/**
* @see net.sf.hibernate.Session#saveOrUpdate(Object)
*/
public static final CascadingAction ACTION_SAVE_UPDATE = new CascadingAction() {
void cascade(SessionImplementor session, Object child, Object anything) throws HibernateException {
log.trace("cascading to saveOrUpdate()");
session.saveOrUpdate(child);
}
Iterator getCascadableChildrenIterator(PersistentCollectionType collectionType, Object collection) {
// saves / updates don't cascade to uninitialized collections
return getLoadedElementsIterator(collectionT