📄 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(collectionType, collection); } boolean deleteOrphans() { // orphans should be deleted during save/update return true; } }; /** * @see net.sf.hibernate.Session#copy(Object) */ public static final CascadingAction ACTION_COPY = new CascadingAction() { void cascade(SessionImplementor session, Object child, Object anything) throws HibernateException { log.trace("cascading to copy()"); session.copy( child, (java.util.Map) anything ); } Iterator getCascadableChildrenIterator(PersistentCollectionType collectionType, Object collection) { // saves / updates don't cascade to uninitialized collections return getLoadedElementsIterator(collectionType, collection); } boolean deleteOrphans() { // orphans should not be deleted during copy?? return false; } }; public static final CascadingAction ACTION_REPLICATE = new CascadingAction() { void cascade(SessionImplementor session, Object child, Object anything) throws HibernateException { log.trace("cascading to replicate()"); session.replicate( child, (ReplicationMode) anything ); } Iterator getCascadableChildrenIterator(PersistentCollectionType collectionType, Object collection) { // replicate does cascade to uninitialized collections return getLoadedElementsIterator(collectionType, collection); } boolean deleteOrphans() { return false; //I suppose? } }; private static boolean collectionIsInitialized(Object collection) { return !(collection instanceof PersistentCollection) || ( (PersistentCollection) collection ).wasInitialized(); } // The types of children to cascade to: /** * A cascade point that occurs just after the insertion of the parent entity and * just before deletion */ public static final int CASCADE_AFTER_INSERT_BEFORE_DELETE = 1; /** * A cascade point that occurs just before the insertion of the parent entity and * just after deletion */ public static final int CASCADE_BEFORE_INSERT_AFTER_DELETE = 2; /** * A cascade point that occurs just after the insertion of the parent entity and * just before deletion, inside a collection */ public static final int CASCADE_AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION = 3; /** * A cascade point that occurs just after update of the parent entity */ public static final int CASCADE_ON_UPDATE = 0; /** * A cascade point that occurs just after eviction of the parent entity from the * session cache */ public static final int CASCADE_ON_EVICT = 0; /** * A cascade point that occurs just after locking a transient parent entity into the * session cache */ public static final int CASCADE_ON_LOCK = 0; /** * A cascade point that occurs just after copying from a transient parent entity into * the object in the session cache */ public static final int CASCADE_ON_COPY = 0; // The allowable cascade styles for a property: /** * A style of cascade that can be specified by the mapping for an association. * The style is specified by the <tt>cascade</tt> attribute in the mapping file. */ public abstract static class CascadeStyle { protected CascadeStyle() {} /** * Should the given action be cascaded? */ abstract boolean doCascade(CascadingAction action); /** * Should we cascade to this particular child? */ /*boolean doCascade(SessionImplementor session, Object child) throws HibernateException { return true; };*/ boolean hasOrphanDelete() { return false; } } /** * save / delete / update / evict / lock / replicate + delete orphans */ public static final CascadeStyle STYLE_ALL_DELETE_ORPHAN = new CascadeStyle() { boolean doCascade(CascadingAction action) { return true; } boolean hasOrphanDelete() { return true; } }; /** * save / delete / update / evict / lock / replicate */ public static final CascadeStyle STYLE_ALL = new CascadeStyle() { boolean doCascade(CascadingAction action) { return true; } }; /** * save / update / lock / replicate */ public static final CascadeStyle STYLE_SAVE_UPDATE = new CascadeStyle() { boolean doCascade(CascadingAction action) { return action==ACTION_SAVE_UPDATE || action==ACTION_LOCK || action==ACTION_REPLICATE || action==ACTION_COPY; } }; /** * save */ /*public static final CascadeStyle STYLE_SAVE = new CascadeStyle() { boolean doCascade(CascadingAction action) { return action==ACTION_SAVE_UPDATE; } boolean doCascade(SessionImplementor session, Object child) throws HibernateException { return !session.isSaved(child); }; };*/ /** * delete */ public static final CascadeStyle STYLE_ONLY_DELETE = new CascadeStyle() { boolean doCascade(CascadingAction action) { return action==ACTION_DELETE; } }; /** * delete + delete orphans */ public static final CascadeStyle STYLE_DELETE_ORPHAN = new CascadeStyle() { boolean doCascade(CascadingAction action) { return action==ACTION_DELETE; } boolean hasOrphanDelete() { return true; } }; /** * no cascades */ public static final CascadeStyle STYLE_NONE = new CascadeStyle() { boolean doCascade(CascadingAction action) { return action==ACTION_REPLICATE; } }; // The allowable unsaved-value settings: /** * A strategy for determining if an identifier value is an identifier of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -