HHH-6172 Introducing CascadeType enum as a replacement for the string 'cascade'

This commit is contained in:
Hardy Ferentschik 2011-05-25 16:47:48 +02:00
parent 8ff2f9d192
commit 2570685399
12 changed files with 185 additions and 32 deletions

View File

@ -37,7 +37,6 @@ import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.relational.DerivedValue;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.relational.Tuple;
import org.hibernate.metamodel.relational.Value;
import org.hibernate.metamodel.relational.state.SimpleValueRelationalState;
@ -61,7 +60,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
private boolean isLazy;
private String propertyAccessorName;
private boolean isAlternateUniqueKey;
private String cascade;
private Set<CascadeType> cascadeTypes;
private boolean optimisticLockable;
// DOM4J specific...
@ -79,7 +78,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
isLazy = state.isLazy();
propertyAccessorName = state.getPropertyAccessorName();
isAlternateUniqueKey = state.isAlternateUniqueKey();
cascade = state.getCascade();
cascadeTypes = state.getCascadeTypes();
optimisticLockable = state.isOptimisticLockable();
nodeName = state.getNodeName();
metaAttributes = state.getMetaAttributes();
@ -150,8 +149,8 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
return hibernateTypeDescriptor;
}
public String getCascade() {
return cascade;
public Set<CascadeType> getCascadeTypes() {
return cascadeTypes;
}
public boolean isOptimisticLockable() {

View File

@ -29,7 +29,6 @@ import java.util.Set;
import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.relational.Value;
/**

View File

@ -0,0 +1,120 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.binding;
import java.util.HashMap;
import java.util.Map;
/**
* @author Hardy Ferentschik
*/
public enum CascadeType {
/**
* Cascades save, delete, update, evict, lock, replicate, merge, persist
*/
ALL,
/**
* Cascades save, delete, update, evict, lock, replicate, merge, persist + delete orphans
*/
ALL_DELETE_ORPHAN,
/**
* Cascades save and update
*/
UPDATE,
/**
* Cascades persist
*/
PERSIST,
/**
* Cascades merge
*/
MERGE,
/**
* Cascades lock
*/
LOCK,
/**
* Cascades refresh
*/
REFRESH,
/**
* Cascades replicate
*/
REPLICATE,
/**
* Cascades evict
*/
EVICT,
/**
* Cascade delete
*/
DELETE,
/**
* Cascade delete + delete orphans
*/
DELETE_ORPHAN,
/**
* No cascading
*/
NONE;
private static final Map<String, CascadeType> hbmOptionToEnum = 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 );
}
/**
* @param hbmOptionName the cascading option as specified in the hbm mapping file
*
* @return Returns the {@code CascadeType} for a given hbm cascading option
*/
public static CascadeType getCascadeType(String hbmOptionName) {
return hbmOptionToEnum.get( hbmOptionName );
}
}

View File

@ -44,7 +44,6 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
private String referencedAttributeName;
private String referencedEntityName;
private AttributeBinding referencedAttributeBinding;
private boolean ignoreNotFound;
ManyToOneAttributeBinding(EntityBinding entityBinding) {
super( entityBinding, false, false );
@ -153,7 +152,7 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
public void validate() {
// can't check this until both the domain and relational states are initialized...
if ( getCascade() != null && getCascade().indexOf( "delete-orphan" ) >= 0 ) {
if ( getCascadeTypes().contains( CascadeType.DELETE_ORPHAN ) ) {
if ( !isLogicalOneToOne ) {
throw new MappingException(
"many-to-one attribute [" + getAttribute().getName() + "] does not support orphan delete as it is not unique"

View File

@ -24,7 +24,9 @@
package org.hibernate.metamodel.binding.state;
import java.util.Map;
import java.util.Set;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.domain.MetaAttribute;
/**
@ -43,7 +45,7 @@ public interface AttributeBindingState {
boolean isAlternateUniqueKey();
String getCascade();
Set<CascadeType> getCascadeTypes();
boolean isOptimisticLockable();

View File

@ -24,8 +24,10 @@
package org.hibernate.metamodel.source.annotations.entity.state.binding;
import java.util.Map;
import java.util.Set;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.source.annotations.entity.SimpleAttribute;
@ -104,7 +106,7 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
}
@Override
public String getCascade() {
public Set<CascadeType> getCascadeTypes() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

View File

@ -23,6 +23,9 @@
*/
package org.hibernate.metamodel.source.annotations.entity.state.binding;
import java.util.Set;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState;
import org.hibernate.metamodel.source.annotations.entity.AssociationAttribute;
@ -43,8 +46,8 @@ public class ManyToOneBindingStateImpl extends AttributeBindingStateImpl impleme
}
@Override
public String getCascade() {
return null; //To change body of implemented methods use File | Settings | File Templates.
public Set<CascadeType> getCascadeTypes() {
return null;
}
@Override

View File

@ -23,14 +23,17 @@
*/
package org.hibernate.metamodel.source.hbm.state.binding;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.MappingException;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.binding.state.AttributeBindingState;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.source.hbm.util.MappingHelper;
/**
@ -73,6 +76,20 @@ public abstract class AbstractHbmAttributeBindingState implements AttributeBindi
return ownerClassName;
}
protected Set<CascadeType> determineCascadeTypes(String cascade) {
String commaSeparatedCascades = MappingHelper.getStringValue( cascade, getDefaults().getDefaultCascade() );
Set<String> cascades = MappingHelper.getStringValueTokens( commaSeparatedCascades, "," );
Set<CascadeType> cascadeTypes = new HashSet<CascadeType>( cascades.size() );
for ( String s : cascades ) {
CascadeType cascadeType = CascadeType.getCascadeType( s );
if ( cascadeType == null ) {
throw new MappingException( "Invalid cascading option " + s );
}
cascadeTypes.add( cascadeType );
}
return cascadeTypes;
}
protected final String getTypeNameByReflection() {
Class ownerClass = MappingHelper.classForName( ownerClassName, defaults.getServiceRegistry() );
return ReflectHelper.reflectedPropertyClass( ownerClass, attributeName ).getName();

View File

@ -23,6 +23,9 @@
*/
package org.hibernate.metamodel.source.hbm.state.binding;
import java.util.Set;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.binding.state.DiscriminatorBindingState;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLDiscriminator;
@ -46,7 +49,7 @@ public class HbmDiscriminatorBindingState extends AbstractHbmAttributeBindingSta
this.discriminator = discriminator;
}
public String getCascade() {
public Set<CascadeType> getCascadeTypes() {
return null;
}

View File

@ -23,16 +23,20 @@
*/
package org.hibernate.metamodel.source.hbm.state.binding;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.source.hbm.HbmHelper;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLManyToOneElement;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.source.hbm.util.MappingHelper;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLManyToOneElement;
/**
* @author Gail Badner
@ -44,7 +48,7 @@ public class HbmManyToOneAttributeBindingState
private final FetchMode fetchMode;
private final boolean isUnwrapProxy;
private final boolean isLazy;
private final String cascade;
private final Set<CascadeType> cascadeTypes;
private final boolean isEmbedded;
private final String referencedPropertyName;
private final String referencedEntityName;
@ -74,7 +78,7 @@ public class HbmManyToOneAttributeBindingState
isLazy = manyToOne.getLazy() == null ||
isUnwrapProxy ||
"proxy".equals( manyToOne.getLazy().value() );
cascade = MappingHelper.getStringValue( manyToOne.getCascade(), defaults.getDefaultCascade() );
cascadeTypes = determineCascadeTypes( manyToOne.getCascade() );
isEmbedded = manyToOne.isEmbedXml();
referencedEntityName = getReferencedEntityName( ownerClassName, manyToOne, defaults );
referencedPropertyName = manyToOne.getPropertyRef();
@ -148,8 +152,8 @@ public class HbmManyToOneAttributeBindingState
return referencedEntityName;
}
public String getCascade() {
return cascade;
public Set<CascadeType> getCascadeTypes() {
return cascadeTypes;
}
public boolean ignoreNotFound() {

View File

@ -27,20 +27,22 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.hibernate.FetchMode;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.binding.CustomSQL;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.binding.state.PluralAttributeBindingState;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.source.hbm.HbmHelper;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.source.hbm.util.MappingHelper;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLBagElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlDeleteAllElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlDeleteElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlInsertElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlUpdateElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSynchronizeElement;
import org.hibernate.metamodel.source.hbm.util.MappingHelper;
/**
* @author Gail Badner
@ -50,7 +52,7 @@ public class HbmPluralAttributeBindingState extends AbstractHbmAttributeBindingS
private final XMLBagElement collection;
private final Class collectionPersisterClass;
private final String typeName;
private final String cascade;
private final Set<CascadeType> cascadeTypes;
public HbmPluralAttributeBindingState(
String ownerClassName,
@ -72,7 +74,7 @@ public class HbmPluralAttributeBindingState extends AbstractHbmAttributeBindingS
this.collectionPersisterClass = MappingHelper.classForName(
collection.getPersister(), getDefaults().getServiceRegistry()
);
this.cascade = MappingHelper.getStringValue( collection.getCascade(), mappingDefaults.getDefaultCascade() );
this.cascadeTypes = determineCascadeTypes( collection.getCascade() );
//Attribute typeNode = collectionElement.attribute( "collection-type" );
//if ( typeNode != null ) {
@ -194,7 +196,8 @@ public class HbmPluralAttributeBindingState extends AbstractHbmAttributeBindingS
public boolean isOrphanDelete() {
// ORPHAN DELETE (used for programmer error detection)
return ( getCascade().indexOf( "delete-orphan" ) >= 0 );
return true;
//return ( getCascade().indexOf( "delete-orphan" ) >= 0 );
}
public int getBatchSize() {
@ -282,8 +285,8 @@ public class HbmPluralAttributeBindingState extends AbstractHbmAttributeBindingS
collection.getLoader().getQueryRef();
}
public String getCascade() {
return cascade;
public Set<CascadeType> getCascadeTypes() {
return cascadeTypes;
}
public boolean isKeyCascadeDeleteEnabled() {

View File

@ -25,19 +25,21 @@ package org.hibernate.metamodel.source.hbm.state.binding;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.hibernate.MappingException;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.binding.CascadeType;
import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.source.hbm.HbmHelper;
import org.hibernate.metamodel.source.hbm.MappingDefaults;
import org.hibernate.metamodel.source.hbm.util.MappingHelper;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLTimestamp;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLVersion;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLParamElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLPropertyElement;
import org.hibernate.metamodel.source.hbm.util.MappingHelper;
/**
* @author Gail Badner
@ -261,7 +263,7 @@ public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingS
return isUpdatable;
}
public String getCascade() {
public Set<CascadeType> getCascadeTypes() {
return null;
}