1 package org.hibernate.engine; 3 4 import java.util.Iterator ; 5 import java.util.Map ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.hibernate.HibernateException; 10 import org.hibernate.LockMode; 11 import org.hibernate.ReplicationMode; 12 import org.hibernate.collection.PersistentCollection; 13 import org.hibernate.event.EventSource; 14 import org.hibernate.type.CollectionType; 15 16 21 public abstract class CascadingAction { 22 23 private static final Log log = LogFactory.getLog(CascadingAction.class); 24 25 28 public abstract void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 29 throws HibernateException; 30 33 public abstract Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection); 34 37 public abstract boolean deleteOrphans(); 38 39 42 public static final CascadingAction DELETE = new CascadingAction() { 43 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 44 throws HibernateException { 45 if ( log.isTraceEnabled() ) log.trace("cascading to delete: " + entityName); 46 if ( ForeignKeys.isNotTransient(entityName, child, null, session) ) { 47 session.delete(entityName, child, isCascadeDeleteEnabled); 48 } 49 } 50 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 51 return CascadingAction.getAllElementsIterator(session, collectionType, collection); 53 } 54 public boolean deleteOrphans() { 55 return true; 57 } 58 public String toString() { 59 return "ACTION_DELETE"; 60 } 61 }; 62 63 66 public static final CascadingAction LOCK = new CascadingAction() { 67 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 68 throws HibernateException { 69 if ( log.isTraceEnabled() ) log.trace("cascading to lock: " + entityName); 70 session.lock( entityName, child, LockMode.NONE ); 71 } 72 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 73 return getLoadedElementsIterator(session, collectionType, collection); 75 } 76 public boolean deleteOrphans() { 77 return false; 79 } 80 public String toString() { 81 return "ACTION_LOCK"; 82 } 83 }; 84 85 88 public static final CascadingAction REFRESH = new CascadingAction() { 89 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 90 throws HibernateException { 91 if ( log.isTraceEnabled() ) log.trace("cascading to refresh: " + entityName); 92 session.refresh( child, (Map ) anything ); 93 } 94 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 95 return getLoadedElementsIterator(session, collectionType, collection); 97 } 98 public boolean deleteOrphans() { 99 return false; 100 } 101 public String toString() { 102 return "ACTION_REFRESH"; 103 } 104 }; 105 106 109 public static final CascadingAction EVICT = new CascadingAction() { 110 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 111 throws HibernateException { 112 if ( log.isTraceEnabled() ) log.trace("cascading to evict: " + entityName); 113 session.evict(child); 114 } 115 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 116 return getLoadedElementsIterator(session, collectionType, collection); 118 } 119 public boolean deleteOrphans() { 120 return false; 121 } 122 public String toString() { 123 return "ACTION_EVICT"; 124 } 125 }; 126 127 130 public static final CascadingAction SAVE_UPDATE = new CascadingAction() { 131 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 132 throws HibernateException { 133 if ( log.isTraceEnabled() ) log.trace("cascading to saveOrUpdate: " + entityName); 134 session.saveOrUpdate(entityName, child); 135 } 136 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 137 return getLoadedElementsIterator(session, collectionType, collection); 139 } 140 public boolean deleteOrphans() { 141 return true; 143 } 144 public String toString() { 145 return "ACTION_SAVE_UPDATE"; 146 } 147 }; 148 149 152 public static final CascadingAction MERGE = new CascadingAction() { 153 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 154 throws HibernateException { 155 if ( log.isTraceEnabled() ) log.trace("cascading to merge: " + entityName); 156 session.merge( entityName, child, (Map ) anything ); 157 } 158 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 159 return getLoadedElementsIterator(session, collectionType, collection); 161 } 162 public boolean deleteOrphans() { 163 return false; 165 } 166 public String toString() { 167 return "ACTION_MERGE"; 168 } 169 }; 170 171 174 public static final CascadingAction SAVE_UPDATE_COPY = new CascadingAction() { 175 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 177 throws HibernateException { 178 if ( log.isTraceEnabled() ) log.trace("cascading to saveOrUpdateCopy: " + entityName); 179 session.saveOrUpdateCopy( entityName, child, (Map ) anything ); 180 } 181 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 182 return getLoadedElementsIterator(session, collectionType, collection); 184 } 185 public boolean deleteOrphans() { 186 return false; 188 } 189 public String toString() { 190 return "ACTION_SAVE_UPDATE_COPY"; 191 } 192 }; 193 194 197 public static final CascadingAction PERSIST = new CascadingAction() { 198 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 199 throws HibernateException { 200 if ( log.isTraceEnabled() ) log.trace("cascading to persist: " + entityName); 201 session.persist( entityName, child, (Map ) anything ); 202 } 203 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 204 return CascadingAction.getAllElementsIterator(session, collectionType, collection); 206 } 207 public boolean deleteOrphans() { 208 return false; 210 } 211 public String toString() { 212 return "ACTION_PERSIST"; 213 } 214 }; 215 216 219 public static final CascadingAction REPLICATE = new CascadingAction() { 220 public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) 221 throws HibernateException { 222 if ( log.isTraceEnabled() ) log.trace("cascading to replicate: " + entityName); 223 session.replicate( entityName, child, (ReplicationMode) anything ); 224 } 225 public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { 226 return getLoadedElementsIterator(session, collectionType, collection); 228 } 229 public boolean deleteOrphans() { 230 return false; } 232 public String toString() { 233 return "ACTION_REPLICATE"; 234 } 235 }; 236 237 CascadingAction() {} 238 239 242 private static Iterator getAllElementsIterator(EventSource session, CollectionType collectionType, Object collection) { 243 return collectionType.getElementsIterator(collection, session); 244 } 245 249 public static Iterator getLoadedElementsIterator(SessionImplementor session, CollectionType collectionType, Object collection) { 250 if ( collectionIsInitialized(collection) ) { 251 return collectionType.getElementsIterator(collection, session); 253 } 254 else { 255 return ( (PersistentCollection) collection ).queuedAdditionIterator(); 258 } 259 } 260 261 private static boolean collectionIsInitialized(Object collection) { 262 return !(collection instanceof PersistentCollection) || ( (PersistentCollection) collection ).wasInitialized(); 263 } 264 } | Popular Tags |