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
* @todo integrate this w/ org.hibernate.engine.spi.CascadeStyle
*/
public enum CascadeType {
/**
@ -91,22 +92,32 @@ public enum CascadeType {
*/
NONE;
private static final Map<String, CascadeType> hbmOptionToEnum = new HashMap<String, CascadeType>();
private static final Map<String, CascadeType> hbmOptionToCascadeType = new HashMap<String, CascadeType>();
static {
hbmOptionToEnum.put( "all", ALL );
hbmOptionToEnum.put( "all-delete-orphan", ALL_DELETE_ORPHAN );
hbmOptionToEnum.put( "save-update", UPDATE );
hbmOptionToEnum.put( "persist", PERSIST );
hbmOptionToEnum.put( "merge", MERGE );
hbmOptionToEnum.put( "lock", LOCK );
hbmOptionToEnum.put( "refresh", REFRESH );
hbmOptionToEnum.put( "replicate", REPLICATE );
hbmOptionToEnum.put( "evict", EVICT );
hbmOptionToEnum.put( "delete", DELETE );
hbmOptionToEnum.put( "remove", DELETE ); // adds remove as a sort-of alias for delete...
hbmOptionToEnum.put( "delete-orphan", DELETE_ORPHAN );
hbmOptionToEnum.put( "none", NONE );
hbmOptionToCascadeType.put( "all", ALL );
hbmOptionToCascadeType.put( "all-delete-orphan", ALL_DELETE_ORPHAN );
hbmOptionToCascadeType.put( "save-update", UPDATE );
hbmOptionToCascadeType.put( "persist", PERSIST );
hbmOptionToCascadeType.put( "merge", MERGE );
hbmOptionToCascadeType.put( "lock", LOCK );
hbmOptionToCascadeType.put( "refresh", REFRESH );
hbmOptionToCascadeType.put( "replicate", REPLICATE );
hbmOptionToCascadeType.put( "evict", EVICT );
hbmOptionToCascadeType.put( "delete", DELETE );
hbmOptionToCascadeType.put( "remove", DELETE ); // adds remove as a sort-of alias for delete...
hbmOptionToCascadeType.put( "delete-orphan", DELETE_ORPHAN );
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
*/
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;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.CascadeType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
@ -41,6 +44,7 @@ public class AssociationAttribute extends SimpleAttribute {
private final AssociationType associationType;
private final boolean ignoreNotFound;
private final String referencedEntityType;
private final Set<CascadeType> cascadeTypes;
public static AssociationAttribute createAssociationAttribute(String name, String type, AssociationType associationType, Map<DotName, List<AnnotationInstance>> annotations) {
return new AssociationAttribute( name, type, associationType, annotations );
@ -57,6 +61,7 @@ public class AssociationAttribute extends SimpleAttribute {
);
referencedEntityType = determineReferencedEntityType( associationAnnotation );
cascadeTypes = determineCascadeTypes( associationAnnotation );
}
public boolean isIgnoreNotFound() {
@ -71,6 +76,10 @@ public class AssociationAttribute extends SimpleAttribute {
return associationType;
}
public Set<CascadeType> getCascadeTypes() {
return cascadeTypes;
}
private boolean ignoreNotFound() {
NotFoundAction action = NotFoundAction.EXCEPTION;
AnnotationInstance notFoundAnnotation = getIfExists( HibernateDotNames.NOT_FOUND );
@ -99,6 +108,18 @@ public class AssociationAttribute extends SimpleAttribute {
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;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.metamodel.binding.CascadeType;
@ -47,7 +48,11 @@ public class ManyToOneBindingStateImpl extends AttributeBindingStateImpl impleme
@Override
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