📄 cascades.java
字号:
//$Id: Cascades.java,v 1.39 2005/05/05 02:41:21 oneovthafew Exp $package org.hibernate.engine;import java.io.Serializable;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.EntityMode;import org.hibernate.HibernateException;import org.hibernate.LockMode;import org.hibernate.MappingException;import org.hibernate.ReplicationMode;import org.hibernate.collection.PersistentCollection;import org.hibernate.id.IdentifierGeneratorFactory;import org.hibernate.persister.collection.CollectionPersister;import org.hibernate.persister.entity.EntityPersister;import org.hibernate.type.AbstractComponentType;import org.hibernate.type.AssociationType;import org.hibernate.type.EntityType;import org.hibernate.type.CollectionType;import org.hibernate.type.Type;import org.hibernate.util.ArrayHelper;/** * Implements cascaded save / delete / update / lock / evict / replicate / persist / merge * * @see org.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, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException; /** * Should this action be cascaded to the given (possibly uninitialized) collection? */ abstract Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection); /** * Do we need to handle orphan delete for this action? */ abstract boolean deleteOrphans(); } /** * @see org.hibernate.Session#delete(Object) */ public static final CascadingAction ACTION_DELETE = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to delete: " + entityName); if ( ForeignKeys.isNotTransient(entityName, child, null, session) ) session.delete(entityName, child, isCascadeDeleteEnabled); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // delete does cascade to uninitialized collections return getAllElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { // orphans should be deleted during delete return true; } public String toString() { return "ACTION_DELETE"; } }; /** * @see org.hibernate.Session#lock(Object, LockMode) */ public static final CascadingAction ACTION_LOCK = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to lock: " + entityName); session.lock( entityName, child, LockMode.NONE/*(LockMode) anything*/ ); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // lock doesn't cascade to uninitialized collections return getLoadedElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { //TODO: should orphans really be deleted during lock??? return false; } public String toString() { return "ACTION_LOCK"; } }; /** * @see org.hibernate.Session#refresh(Object) */ public static final CascadingAction ACTION_REFRESH = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to refresh: " + entityName); session.refresh(child); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // refresh doesn't cascade to uninitialized collections return getLoadedElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { return false; } public String toString() { return "ACTION_REFRESH"; } }; /** * @see org.hibernate.Session#evict(Object) */ public static final CascadingAction ACTION_EVICT = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to evict: " + entityName); session.evict(child); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // evicts don't cascade to uninitialized collections return getLoadedElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { return false; } public String toString() { return "ACTION_EVICT"; } }; /** * @see org.hibernate.Session#saveOrUpdate(Object) */ public static final CascadingAction ACTION_SAVE_UPDATE = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to saveOrUpdate: " + entityName); session.saveOrUpdate(entityName, child); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // saves / updates don't cascade to uninitialized collections return getLoadedElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { // orphans should be deleted during save/update return true; } public String toString() { return "ACTION_SAVE_UPDATE"; } }; /** * @see org.hibernate.Session#merge(Object) */ public static final CascadingAction ACTION_MERGE = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to merge: " + entityName); session.merge( entityName, child, (Map) anything ); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // saves / updates don't cascade to uninitialized collections return getLoadedElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { // orphans should not be deleted during copy?? return false; } public String toString() { return "ACTION_MERGE"; } }; /** * @see org.hibernate.classic.Session#saveOrUpdateCopy(Object) */ public static final CascadingAction ACTION_SAVE_UPDATE_COPY = new CascadingAction() { // for deprecated saveOrUpdateCopy() void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to saveOrUpdateCopy: " + entityName); session.saveOrUpdateCopy( entityName, child, (Map) anything ); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // saves / updates don't cascade to uninitialized collections return getLoadedElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { // orphans should not be deleted during copy?? return false; } public String toString() { return "ACTION_SAVE_UPDATE_COPY"; } }; /** * @see org.hibernate.Session#persist(Object) */ public static final CascadingAction ACTION_PERSIST = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to persist: " + entityName); session.persist( entityName, child, (Map) anything ); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // saves / updates don't cascade to uninitialized collections return getAllElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { // orphans should not be deleted during create return false; } public String toString() { return "ACTION_PERSIST"; } }; public static final CascadingAction ACTION_REPLICATE = new CascadingAction() { void cascade(SessionImplementor session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) throws HibernateException { if ( log.isTraceEnabled() ) log.trace("cascading to replicate: " + entityName); session.replicate( entityName, child, (ReplicationMode) anything ); } Iterator getCascadableChildrenIterator(SessionImplementor session, CollectionType collectionType, Object collection) { // replicate does cascade to uninitialized collections return getLoadedElementsIterator(session, collectionType, collection); } boolean deleteOrphans() { return false; //I suppose? } public String toString() { return "ACTION_REPLICATE"; } }; 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_AFTER_UPDATE = 0; /** * A cascade point that occurs just before the session is flushed */ public static final int CASCADE_BEFORE_FLUSH = 0; /** * A cascade point that occurs just after eviction of the parent entity from the * session cache */ public static final int CASCADE_AFTER_EVICT = 0; /** * A cascade point that occurs just after locking a transient parent entity into the * session cache */ public static final int CASCADE_AFTER_REFRESH = 0; /** * A cascade point that occurs just after refreshing a parent entity */ public static final int CASCADE_AFTER_LOCK = 0; /** * A cascade point that occurs just before merging from a transient parent entity into * the object in the session cache */ public static final int CASCADE_BEFORE_MERGE = 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 implements Serializable { protected CascadeStyle() {} /** * Should the given action be cascaded? */ abstract boolean doCascade(CascadingAction action); /** * Should the given action really, really be cascaded? */ boolean reallyDoCascade(CascadingAction action) { return doCascade(action); } /** * Do we need to delete orphaned collection elements? */ boolean hasOrphanDelete() { return false; } } public static final class MultipleCascadeStyle extends CascadeStyle { private final CascadeStyle[] styles; public MultipleCascadeStyle(CascadeStyle[] styles) { this.styles = styles; } boolean doCascade(CascadingAction action) { for (int i=0; i<styles.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -