26
26
import java .util .HashMap ;
27
27
import java .util .Map ;
28
28
29
+ import org .hibernate .MappingException ;
30
+ import org .hibernate .engine .spi .CascadeStyle ;
31
+
29
32
30
33
/**
31
34
* @author Hardy Ferentschik
@@ -120,6 +123,22 @@ public enum CascadeType {
120
123
jpaCascadeTypeToHibernateCascadeType .put ( javax .persistence .CascadeType .DETACH , EVICT );
121
124
}
122
125
126
+ private static final Map <CascadeType , CascadeStyle > cascadeTypeToCascadeStyle = new HashMap <CascadeType , CascadeStyle >();
127
+ static {
128
+ cascadeTypeToCascadeStyle .put ( ALL , CascadeStyle .ALL );
129
+ cascadeTypeToCascadeStyle .put ( ALL_DELETE_ORPHAN , CascadeStyle .ALL_DELETE_ORPHAN );
130
+ cascadeTypeToCascadeStyle .put ( UPDATE , CascadeStyle .UPDATE );
131
+ cascadeTypeToCascadeStyle .put ( PERSIST , CascadeStyle .PERSIST );
132
+ cascadeTypeToCascadeStyle .put ( MERGE , CascadeStyle .MERGE );
133
+ cascadeTypeToCascadeStyle .put ( LOCK , CascadeStyle .LOCK );
134
+ cascadeTypeToCascadeStyle .put ( REFRESH , CascadeStyle .REFRESH );
135
+ cascadeTypeToCascadeStyle .put ( REPLICATE , CascadeStyle .REPLICATE );
136
+ cascadeTypeToCascadeStyle .put ( EVICT , CascadeStyle .EVICT );
137
+ cascadeTypeToCascadeStyle .put ( DELETE , CascadeStyle .DELETE );
138
+ cascadeTypeToCascadeStyle .put ( DELETE_ORPHAN , CascadeStyle .DELETE_ORPHAN );
139
+ cascadeTypeToCascadeStyle .put ( NONE , CascadeStyle .NONE );
140
+ }
141
+
123
142
/**
124
143
* @param hbmOptionName the cascading option as specified in the hbm mapping file
125
144
*
@@ -137,4 +156,17 @@ public static CascadeType getCascadeType(String hbmOptionName) {
137
156
public static CascadeType getCascadeType (javax .persistence .CascadeType jpaCascade ) {
138
157
return jpaCascadeTypeToHibernateCascadeType .get ( jpaCascade );
139
158
}
159
+
160
+ /**
161
+ * @return Returns the {@code CascadeStyle} that corresponds to this {@code CascadeType}
162
+ *
163
+ * @throws MappingException if there is not corresponding {@code CascadeStyle}
164
+ */
165
+ public CascadeStyle toCascadeStyle () {
166
+ CascadeStyle cascadeStyle = cascadeTypeToCascadeStyle .get ( this );
167
+ if ( cascadeStyle == null ) {
168
+ throw new MappingException ( "No CascadeStyle that corresponds with CascadeType=" + this .name () );
169
+ }
170
+ return cascadeStyle ;
171
+ }
140
172
}
0 commit comments