HHH-6172 Parsing cascade type in AssociationAttribute

This commit is contained in:
Hardy Ferentschik 2011-05-25 17:20:45 +02:00
parent 2570685399
commit e61896454b
3 changed files with 62 additions and 16 deletions

View File

@ -29,6 +29,7 @@ import java.util.Map;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
* @todo integrate this w/ org.hibernate.engine.spi.CascadeStyle
*/ */
public enum CascadeType { public enum CascadeType {
/** /**
@ -91,22 +92,32 @@ public enum CascadeType {
*/ */
NONE; NONE;
private static final Map<String, CascadeType> hbmOptionToEnum = new HashMap<String, CascadeType>(); private static final Map<String, CascadeType> hbmOptionToCascadeType = new HashMap<String, CascadeType>();
static { static {
hbmOptionToEnum.put( "all", ALL ); hbmOptionToCascadeType.put( "all", ALL );
hbmOptionToEnum.put( "all-delete-orphan", ALL_DELETE_ORPHAN ); hbmOptionToCascadeType.put( "all-delete-orphan", ALL_DELETE_ORPHAN );
hbmOptionToEnum.put( "save-update", UPDATE ); hbmOptionToCascadeType.put( "save-update", UPDATE );
hbmOptionToEnum.put( "persist", PERSIST ); hbmOptionToCascadeType.put( "persist", PERSIST );
hbmOptionToEnum.put( "merge", MERGE ); hbmOptionToCascadeType.put( "merge", MERGE );
hbmOptionToEnum.put( "lock", LOCK ); hbmOptionToCascadeType.put( "lock", LOCK );
hbmOptionToEnum.put( "refresh", REFRESH ); hbmOptionToCascadeType.put( "refresh", REFRESH );
hbmOptionToEnum.put( "replicate", REPLICATE ); hbmOptionToCascadeType.put( "replicate", REPLICATE );
hbmOptionToEnum.put( "evict", EVICT ); hbmOptionToCascadeType.put( "evict", EVICT );
hbmOptionToEnum.put( "delete", DELETE ); hbmOptionToCascadeType.put( "delete", DELETE );
hbmOptionToEnum.put( "remove", DELETE ); // adds remove as a sort-of alias for delete... hbmOptionToCascadeType.put( "remove", DELETE ); // adds remove as a sort-of alias for delete...
hbmOptionToEnum.put( "delete-orphan", DELETE_ORPHAN ); hbmOptionToCascadeType.put( "delete-orphan", DELETE_ORPHAN );
hbmOptionToEnum.put( "none", NONE ); hbmOptionToCascadeType.put( "none", NONE );
}
private static final Map<javax.persistence.CascadeType, CascadeType> jpaCascadeTypeToHibernateCascadeType = new HashMap<javax.persistence.CascadeType, CascadeType>();
static {
jpaCascadeTypeToHibernateCascadeType.put( javax.persistence.CascadeType.ALL, ALL );
jpaCascadeTypeToHibernateCascadeType.put( javax.persistence.CascadeType.PERSIST, PERSIST );
jpaCascadeTypeToHibernateCascadeType.put( javax.persistence.CascadeType.MERGE, MERGE );
jpaCascadeTypeToHibernateCascadeType.put( javax.persistence.CascadeType.REFRESH, REFRESH );
jpaCascadeTypeToHibernateCascadeType.put( javax.persistence.CascadeType.DETACH, EVICT );
} }
/** /**
@ -115,6 +126,15 @@ public enum CascadeType {
* @return Returns the {@code CascadeType} for a given hbm cascading option * @return Returns the {@code CascadeType} for a given hbm cascading option
*/ */
public static CascadeType getCascadeType(String hbmOptionName) { public static CascadeType getCascadeType(String hbmOptionName) {
return hbmOptionToEnum.get( hbmOptionName ); return hbmOptionToCascadeType.get( hbmOptionName );
}
/**
* @param jpaCascade the jpa cascade type
*
* @return Returns the Hibernate {@code CascadeType} for a given jpa cascade type
*/
public static CascadeType getCascadeType(javax.persistence.CascadeType jpaCascade) {
return jpaCascadeTypeToHibernateCascadeType.get( jpaCascade );
} }
} }

View File

@ -23,8 +23,11 @@
*/ */
package org.hibernate.metamodel.source.annotations.entity; package org.hibernate.metamodel.source.annotations.entity;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.persistence.CascadeType;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.AnnotationValue;
@ -41,6 +44,7 @@ public class AssociationAttribute extends SimpleAttribute {
private final AssociationType associationType; private final AssociationType associationType;
private final boolean ignoreNotFound; private final boolean ignoreNotFound;
private final String referencedEntityType; private final String referencedEntityType;
private final Set<CascadeType> cascadeTypes;
public static AssociationAttribute createAssociationAttribute(String name, String type, AssociationType associationType, Map<DotName, List<AnnotationInstance>> annotations) { public static AssociationAttribute createAssociationAttribute(String name, String type, AssociationType associationType, Map<DotName, List<AnnotationInstance>> annotations) {
return new AssociationAttribute( name, type, associationType, annotations ); return new AssociationAttribute( name, type, associationType, annotations );
@ -57,6 +61,7 @@ public class AssociationAttribute extends SimpleAttribute {
); );
referencedEntityType = determineReferencedEntityType( associationAnnotation ); referencedEntityType = determineReferencedEntityType( associationAnnotation );
cascadeTypes = determineCascadeTypes( associationAnnotation );
} }
public boolean isIgnoreNotFound() { public boolean isIgnoreNotFound() {
@ -71,6 +76,10 @@ public class AssociationAttribute extends SimpleAttribute {
return associationType; return associationType;
} }
public Set<CascadeType> getCascadeTypes() {
return cascadeTypes;
}
private boolean ignoreNotFound() { private boolean ignoreNotFound() {
NotFoundAction action = NotFoundAction.EXCEPTION; NotFoundAction action = NotFoundAction.EXCEPTION;
AnnotationInstance notFoundAnnotation = getIfExists( HibernateDotNames.NOT_FOUND ); AnnotationInstance notFoundAnnotation = getIfExists( HibernateDotNames.NOT_FOUND );
@ -99,6 +108,18 @@ public class AssociationAttribute extends SimpleAttribute {
return targetTypeName; return targetTypeName;
} }
private Set<CascadeType> determineCascadeTypes(AnnotationInstance associationAnnotation) {
Set<CascadeType> cascadeTypes = new HashSet<CascadeType>();
AnnotationValue cascadeValue = associationAnnotation.value( "cascade" );
if ( cascadeValue != null ) {
String[] cascades = cascadeValue.asEnumArray();
for ( String s : cascades ) {
cascadeTypes.add( Enum.valueOf( CascadeType.class, s ) );
}
}
return cascadeTypes;
}
} }

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.metamodel.source.annotations.entity.state.binding; package org.hibernate.metamodel.source.annotations.entity.state.binding;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.hibernate.metamodel.binding.CascadeType; import org.hibernate.metamodel.binding.CascadeType;
@ -47,7 +48,11 @@ public class ManyToOneBindingStateImpl extends AttributeBindingStateImpl impleme
@Override @Override
public Set<CascadeType> getCascadeTypes() { public Set<CascadeType> getCascadeTypes() {
return null; Set<CascadeType> hibernateCascadeTypes = new HashSet<CascadeType>();
for ( javax.persistence.CascadeType jpaCascadeType : associationAttribute.getCascadeTypes() ) {
hibernateCascadeTypes.add( CascadeType.getCascadeType( jpaCascadeType ) );
}
return hibernateCascadeTypes;
} }
@Override @Override