1 package org.hibernate.engine; 3 4 import java.io.Serializable ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 8 import org.hibernate.MappingException; 9 import org.hibernate.util.ArrayHelper; 10 11 17 public abstract class CascadeStyle implements Serializable { 18 19 22 public abstract boolean doCascade(CascadingAction action); 23 24 27 public boolean reallyDoCascade(CascadingAction action) { 28 return doCascade(action); 29 } 30 31 34 public boolean hasOrphanDelete() { 35 return false; 36 } 37 38 public static final class MultipleCascadeStyle extends CascadeStyle { 39 private final CascadeStyle[] styles; 40 public MultipleCascadeStyle(CascadeStyle[] styles) { 41 this.styles = styles; 42 } 43 public boolean doCascade(CascadingAction action) { 44 for (int i=0; i<styles.length; i++) { 45 if ( styles[i].doCascade(action) ) return true; 46 } 47 return false; 48 } 49 public boolean reallyDoCascade(CascadingAction action) { 50 for (int i=0; i<styles.length; i++) { 51 if ( styles[i].reallyDoCascade(action) ) return true; 52 } 53 return false; 54 } 55 public boolean hasOrphanDelete() { 56 for (int i=0; i<styles.length; i++) { 57 if ( styles[i].hasOrphanDelete() ) return true; 58 } 59 return false; 60 } 61 public String toString() { 62 return ArrayHelper.toString(styles); 63 } 64 } 65 66 69 public static final CascadeStyle ALL_DELETE_ORPHAN = new CascadeStyle() { 70 public boolean doCascade(CascadingAction action) { 71 return true; 72 } 73 public boolean hasOrphanDelete() { 74 return true; 75 } 76 public String toString() { 77 return "STYLE_ALL_DELETE_ORPHAN"; 78 } 79 }; 80 81 84 public static final CascadeStyle ALL = new CascadeStyle() { 85 public boolean doCascade(CascadingAction action) { 86 return true; 87 } 88 public String toString() { 89 return "STYLE_ALL"; 90 } 91 }; 92 93 96 public static final CascadeStyle UPDATE = new CascadeStyle() { 97 public boolean doCascade(CascadingAction action) { 98 return action==CascadingAction.SAVE_UPDATE || action==CascadingAction.SAVE_UPDATE_COPY; 99 } 100 public String toString() { 101 return "STYLE_SAVE_UPDATE"; 102 } 103 }; 104 105 108 public static final CascadeStyle LOCK = new CascadeStyle() { 109 public boolean doCascade(CascadingAction action) { 110 return action==CascadingAction.LOCK; 111 } 112 public String toString() { 113 return "STYLE_LOCK"; 114 } 115 }; 116 117 120 public static final CascadeStyle REFRESH = new CascadeStyle() { 121 public boolean doCascade(CascadingAction action) { 122 return action==CascadingAction.REFRESH; 123 } 124 public String toString() { 125 return "STYLE_REFRESH"; 126 } 127 }; 128 129 132 public static final CascadeStyle EVICT = new CascadeStyle() { 133 public boolean doCascade(CascadingAction action) { 134 return action==CascadingAction.EVICT; 135 } 136 public String toString() { 137 return "STYLE_EVICT"; 138 } 139 }; 140 141 144 public static final CascadeStyle REPLICATE = new CascadeStyle() { 145 public boolean doCascade(CascadingAction action) { 146 return action==CascadingAction.REPLICATE; 147 } 148 public String toString() { 149 return "STYLE_REPLICATE"; 150 } 151 }; 152 155 public static final CascadeStyle MERGE = new CascadeStyle() { 156 public boolean doCascade(CascadingAction action) { 157 return action==CascadingAction.MERGE; 158 } 159 public String toString() { 160 return "STYLE_MERGE"; 161 } 162 }; 163 164 167 public static final CascadeStyle PERSIST = new CascadeStyle() { 168 public boolean doCascade(CascadingAction action) { 169 return action==CascadingAction.PERSIST; 170 } 171 public String toString() { 172 return "STYLE_PERSIST"; 173 } 174 }; 175 176 179 public static final CascadeStyle DELETE = new CascadeStyle() { 180 public boolean doCascade(CascadingAction action) { 181 return action==CascadingAction.DELETE; 182 } 183 public String toString() { 184 return "STYLE_DELETE"; 185 } 186 }; 187 188 191 public static final CascadeStyle DELETE_ORPHAN = new CascadeStyle() { 192 public boolean doCascade(CascadingAction action) { 193 return action==CascadingAction.DELETE || action==CascadingAction.SAVE_UPDATE; 194 } 195 public boolean reallyDoCascade(CascadingAction action) { 196 return action==CascadingAction.DELETE; 197 } 198 public boolean hasOrphanDelete() { 199 return true; 200 } 201 public String toString() { 202 return "STYLE_DELETE_ORPHAN"; 203 } 204 }; 205 206 209 public static final CascadeStyle NONE = new CascadeStyle() { 210 public boolean doCascade(CascadingAction action) { 211 return false; 212 } 213 public String toString() { 214 return "STYLE_NONE"; 215 } 216 }; 217 218 CascadeStyle() {} 219 220 static final Map STYLES = new HashMap (); 221 static { 222 STYLES.put("all", ALL); 223 STYLES.put("all-delete-orphan", ALL_DELETE_ORPHAN); 224 STYLES.put("save-update", UPDATE); 225 STYLES.put("persist", PERSIST); 226 STYLES.put("merge", MERGE); 227 STYLES.put("lock", LOCK); 228 STYLES.put("refresh", REFRESH); 229 STYLES.put("replicate", REPLICATE); 230 STYLES.put("evict", EVICT); 231 STYLES.put("delete", DELETE); 232 STYLES.put("delete-orphan", DELETE_ORPHAN); 233 STYLES.put("none", NONE); 234 } 235 236 public static CascadeStyle getCascadeStyle(String cascade) { 237 CascadeStyle style = (CascadeStyle) STYLES.get(cascade); 238 if (style==null) { 239 throw new MappingException("Unsupported cascade style: " + cascade); 240 } 241 else { 242 return style; 243 } 244 } 245 } | Popular Tags |