diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java index fe9e7e71af..f13ea17765 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java @@ -29,7 +29,7 @@ import org.hibernate.EntityMode; import org.hibernate.HibernateException; import org.hibernate.PropertyValueException; import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer; -import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.CollectionType; @@ -151,7 +151,7 @@ public final class Nullability { //check for all components values in the collection CompositeType componentType = (CompositeType) collectionElementType; - Iterator iter = CascadingAction.getLoadedElementsIterator( session, collectionType, value ); + Iterator iter = CascadingActions.getLoadedElementsIterator( session, collectionType, value ); while ( iter.hasNext() ) { Object compValue = iter.next(); if (compValue != null) { diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java index c39f0f6840..3daccaeb89 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyle.java @@ -24,20 +24,16 @@ package org.hibernate.engine.spi; import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - -import org.hibernate.MappingException; -import org.hibernate.internal.util.collections.ArrayHelper; /** * A contract for defining the aspects of cascading various persistence actions. * * @author Gavin King + * @author Steve Ebersole + * * @see CascadingAction */ -public abstract class CascadeStyle implements Serializable { - +public interface CascadeStyle extends Serializable { /** * For this style, should the given action be cascaded? * @@ -45,7 +41,7 @@ public abstract class CascadeStyle implements Serializable { * * @return True if the action should be cascaded under this style; false otherwise. */ - public abstract boolean doCascade(CascadingAction action); + public boolean doCascade(CascadingAction action); /** * Probably more aptly named something like doCascadeToCollectionElements(); it is @@ -61,9 +57,7 @@ public abstract class CascadeStyle implements Serializable { * @return True if the action should be really cascaded under this style; * false otherwise. */ - public boolean reallyDoCascade(CascadingAction action) { - return doCascade( action ); - } + public boolean reallyDoCascade(CascadingAction action); /** * Do we need to delete orphaned collection elements? @@ -71,252 +65,5 @@ public abstract class CascadeStyle implements Serializable { * @return True if this style need to account for orphan delete * operations; false otherwise. */ - public boolean hasOrphanDelete() { - return false; - } - - public static final class MultipleCascadeStyle extends CascadeStyle { - private final CascadeStyle[] styles; - - public MultipleCascadeStyle(CascadeStyle[] styles) { - this.styles = styles; - } - - public boolean doCascade(CascadingAction action) { - for ( CascadeStyle style : styles ) { - if ( style.doCascade( action ) ) { - return true; - } - } - return false; - } - - public boolean reallyDoCascade(CascadingAction action) { - for ( CascadeStyle style : styles ) { - if ( style.reallyDoCascade( action ) ) { - return true; - } - } - return false; - } - - public boolean hasOrphanDelete() { - for ( CascadeStyle style : styles ) { - if ( style.hasOrphanDelete() ) { - return true; - } - } - return false; - } - - public String toString() { - return ArrayHelper.toString( styles ); - } - } - - /** - * save / delete / update / evict / lock / replicate / merge / persist + delete orphans - */ - public static final CascadeStyle ALL_DELETE_ORPHAN = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return true; - } - - public boolean hasOrphanDelete() { - return true; - } - - public String toString() { - return "STYLE_ALL_DELETE_ORPHAN"; - } - }; - - /** - * save / delete / update / evict / lock / replicate / merge / persist - */ - public static final CascadeStyle ALL = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return true; - } - - public String toString() { - return "STYLE_ALL"; - } - }; - - /** - * save / update - */ - public static final CascadeStyle UPDATE = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.SAVE_UPDATE; - } - - public String toString() { - return "STYLE_SAVE_UPDATE"; - } - }; - - /** - * lock - */ - public static final CascadeStyle LOCK = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.LOCK; - } - - public String toString() { - return "STYLE_LOCK"; - } - }; - - /** - * refresh - */ - public static final CascadeStyle REFRESH = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.REFRESH; - } - - public String toString() { - return "STYLE_REFRESH"; - } - }; - - /** - * evict - */ - public static final CascadeStyle EVICT = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.EVICT; - } - - public String toString() { - return "STYLE_EVICT"; - } - }; - - /** - * replicate - */ - public static final CascadeStyle REPLICATE = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.REPLICATE; - } - - public String toString() { - return "STYLE_REPLICATE"; - } - }; - /** - * merge - */ - public static final CascadeStyle MERGE = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.MERGE; - } - - public String toString() { - return "STYLE_MERGE"; - } - }; - - /** - * create - */ - public static final CascadeStyle PERSIST = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.PERSIST - || action == CascadingAction.PERSIST_ON_FLUSH; - } - - public String toString() { - return "STYLE_PERSIST"; - } - }; - - /** - * delete - */ - public static final CascadeStyle DELETE = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.DELETE; - } - - public String toString() { - return "STYLE_DELETE"; - } - }; - - /** - * delete + delete orphans - */ - public static final CascadeStyle DELETE_ORPHAN = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action == CascadingAction.DELETE || action == CascadingAction.SAVE_UPDATE; - } - - public boolean reallyDoCascade(CascadingAction action) { - return action == CascadingAction.DELETE; - } - - public boolean hasOrphanDelete() { - return true; - } - - public String toString() { - return "STYLE_DELETE_ORPHAN"; - } - }; - - /** - * no cascades - */ - public static final CascadeStyle NONE = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return false; - } - - public String toString() { - return "STYLE_NONE"; - } - }; - - public CascadeStyle() { - } - - static final Map STYLES = new HashMap(); - - static { - STYLES.put( "all", ALL ); - STYLES.put( "all-delete-orphan", ALL_DELETE_ORPHAN ); - STYLES.put( "save-update", UPDATE ); - STYLES.put( "persist", PERSIST ); - STYLES.put( "merge", MERGE ); - STYLES.put( "lock", LOCK ); - STYLES.put( "refresh", REFRESH ); - STYLES.put( "replicate", REPLICATE ); - STYLES.put( "evict", EVICT ); - STYLES.put( "delete", DELETE ); - STYLES.put( "remove", DELETE ); // adds remove as a sort-of alias for delete... - STYLES.put( "delete-orphan", DELETE_ORPHAN ); - STYLES.put( "none", NONE ); - } - - /** - * Factory method for obtaining named cascade styles - * - * @param cascade The named cascade style name. - * - * @return The appropriate CascadeStyle - */ - public static CascadeStyle getCascadeStyle(String cascade) { - CascadeStyle style = STYLES.get( cascade ); - if ( style == null ) { - throw new MappingException( "Unsupported cascade style: " + cascade ); - } - else { - return style; - } - } + public boolean hasOrphanDelete(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyles.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyles.java new file mode 100644 index 0000000000..10ae5eefcd --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadeStyles.java @@ -0,0 +1,348 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, 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.engine.spi; + +import java.util.HashMap; +import java.util.Map; + +import org.jboss.logging.Logger; + +import org.hibernate.MappingException; +import org.hibernate.internal.util.collections.ArrayHelper; + +/** + * @author Steve Ebersole + */ +public class CascadeStyles { + private static final Logger log = Logger.getLogger( CascadeStyles.class ); + + /** + * Disallow instantiation + */ + private CascadeStyles() { + } + + /** + * save / delete / update / evict / lock / replicate / merge / persist + delete orphans + */ + public static final CascadeStyle ALL_DELETE_ORPHAN = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return true; + } + + @Override + public boolean hasOrphanDelete() { + return true; + } + + @Override + public String toString() { + return "STYLE_ALL_DELETE_ORPHAN"; + } + }; + + /** + * save / delete / update / evict / lock / replicate / merge / persist + */ + public static final CascadeStyle ALL = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return true; + } + + @Override + public String toString() { + return "STYLE_ALL"; + } + }; + + /** + * save / update + */ + public static final CascadeStyle UPDATE = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.SAVE_UPDATE; + } + + @Override + public String toString() { + return "STYLE_SAVE_UPDATE"; + } + }; + + /** + * lock + */ + public static final CascadeStyle LOCK = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.LOCK; + } + + @Override + public String toString() { + return "STYLE_LOCK"; + } + }; + + /** + * refresh + */ + public static final CascadeStyle REFRESH = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.REFRESH; + } + + @Override + public String toString() { + return "STYLE_REFRESH"; + } + }; + + /** + * evict + */ + public static final CascadeStyle EVICT = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.EVICT; + } + + @Override + public String toString() { + return "STYLE_EVICT"; + } + }; + + /** + * replicate + */ + public static final CascadeStyle REPLICATE = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.REPLICATE; + } + + @Override + public String toString() { + return "STYLE_REPLICATE"; + } + }; + + /** + * merge + */ + public static final CascadeStyle MERGE = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.MERGE; + } + + @Override + public String toString() { + return "STYLE_MERGE"; + } + }; + + /** + * create + */ + public static final CascadeStyle PERSIST = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.PERSIST + || action == CascadingActions.PERSIST_ON_FLUSH; + } + + @Override + public String toString() { + return "STYLE_PERSIST"; + } + }; + + /** + * delete + */ + public static final CascadeStyle DELETE = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.DELETE; + } + + @Override + public String toString() { + return "STYLE_DELETE"; + } + }; + + /** + * delete + delete orphans + */ + public static final CascadeStyle DELETE_ORPHAN = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == CascadingActions.DELETE || action == CascadingActions.SAVE_UPDATE; + } + + @Override + public boolean reallyDoCascade(CascadingAction action) { + return action == CascadingActions.DELETE; + } + + @Override + public boolean hasOrphanDelete() { + return true; + } + + @Override + public String toString() { + return "STYLE_DELETE_ORPHAN"; + } + }; + + /** + * no cascades + */ + public static final CascadeStyle NONE = new BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return false; + } + + @Override + public String toString() { + return "STYLE_NONE"; + } + }; + + private static final Map STYLES = buildBaseCascadeStyleMap(); + + private static Map buildBaseCascadeStyleMap() { + final HashMap base = new HashMap(); + + base.put( "all", ALL ); + base.put( "all-delete-orphan", ALL_DELETE_ORPHAN ); + base.put( "save-update", UPDATE ); + base.put( "persist", PERSIST ); + base.put( "merge", MERGE ); + base.put( "lock", LOCK ); + base.put( "refresh", REFRESH ); + base.put( "replicate", REPLICATE ); + base.put( "evict", EVICT ); + base.put( "delete", DELETE ); + base.put( "remove", DELETE ); // adds remove as a sort-of alias for delete... + base.put( "delete-orphan", DELETE_ORPHAN ); + base.put( "none", NONE ); + + return base; + } + + /** + * Factory method for obtaining named cascade styles + * + * @param cascade The named cascade style name. + * + * @return The appropriate CascadeStyle + */ + public static CascadeStyle getCascadeStyle(String cascade) { + CascadeStyle style = STYLES.get( cascade ); + if ( style == null ) { + throw new MappingException( "Unsupported cascade style: " + cascade ); + } + else { + return style; + } + } + + public static void registerCascadeStyle(String name, BaseCascadeStyle cascadeStyle) { + log.tracef( "Registering external cascade style [%s : %s]", name, cascadeStyle ); + final CascadeStyle old = STYLES.put( name, cascadeStyle ); + if ( old != null ) { + log.debugf( + "External cascade style regsitration [%s : %s] overrode base registration [%s]", + name, + cascadeStyle, + old + ); + } + } + + public static abstract class BaseCascadeStyle implements CascadeStyle { + @Override + public boolean reallyDoCascade(CascadingAction action) { + return doCascade( action ); + } + + @Override + public boolean hasOrphanDelete() { + return false; + } + } + + public static final class MultipleCascadeStyle extends BaseCascadeStyle { + private final CascadeStyle[] styles; + + public MultipleCascadeStyle(CascadeStyle[] styles) { + this.styles = styles; + } + + @Override + public boolean doCascade(CascadingAction action) { + for ( CascadeStyle style : styles ) { + if ( style.doCascade( action ) ) { + return true; + } + } + return false; + } + + @Override + public boolean reallyDoCascade(CascadingAction action) { + for ( CascadeStyle style : styles ) { + if ( style.reallyDoCascade( action ) ) { + return true; + } + } + return false; + } + + @Override + public boolean hasOrphanDelete() { + for ( CascadeStyle style : styles ) { + if ( style.hasOrphanDelete() ) { + return true; + } + } + return false; + } + + @Override + public String toString() { + return ArrayHelper.toString( styles ); + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingAction.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingAction.java index 5ed0c4012c..3a467f5691 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingAction.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingAction.java @@ -24,40 +24,19 @@ package org.hibernate.engine.spi; import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import org.jboss.logging.Logger; import org.hibernate.HibernateException; -import org.hibernate.LockMode; -import org.hibernate.LockOptions; -import org.hibernate.ReplicationMode; -import org.hibernate.TransientPropertyValueException; -import org.hibernate.collection.spi.PersistentCollection; -import org.hibernate.engine.internal.ForeignKeys; import org.hibernate.event.spi.EventSource; -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.proxy.HibernateProxy; import org.hibernate.type.CollectionType; -import org.hibernate.type.EntityType; -import org.hibernate.type.Type; /** * A session action that may be cascaded from parent entity to its children * * @author Gavin King + * @author Steve Ebersole */ -public abstract class CascadingAction { - - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, CascadingAction.class.getName()); - - - // the CascadingAction contract ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - public CascadingAction() { - } +public interface CascadingAction { /** * Cascade the action to the child object. @@ -70,7 +49,7 @@ public abstract class CascadingAction { * @param isCascadeDeleteEnabled Are cascading deletes enabled. * @throws HibernateException */ - public abstract void cascade( + public void cascade( EventSource session, Object child, String entityName, @@ -86,7 +65,7 @@ public abstract class CascadingAction { * @param collection The collection instance. * @return The children iterator. */ - public abstract Iterator getCascadableChildrenIterator( + public Iterator getCascadableChildrenIterator( EventSource session, CollectionType collectionType, Object collection); @@ -96,7 +75,7 @@ public abstract class CascadingAction { * * @return True if this action can lead to deletions of orphans. */ - public abstract boolean deleteOrphans(); + public boolean deleteOrphans(); /** @@ -104,9 +83,7 @@ public abstract class CascadingAction { * * @return True if this action requires no-cascade verification; false otherwise. */ - public boolean requiresNoCascadeChecking() { - return false; - } + public boolean requiresNoCascadeChecking(); /** * Called (in the case of {@link #requiresNoCascadeChecking} returning true) to validate @@ -118,357 +95,10 @@ public abstract class CascadingAction { * @param persister The entity persister for the owner * @param propertyIndex The index of the property within the owner. */ - public void noCascade(EventSource session, Object child, Object parent, EntityPersister persister, int propertyIndex) { - } + public void noCascade(EventSource session, Object child, Object parent, EntityPersister persister, int propertyIndex); /** * Should this action be performed (or noCascade consulted) in the case of lazy properties. */ - public boolean performOnLazyProperty() { - return true; - } - - - // the CascadingAction implementations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - /** - * @see org.hibernate.Session#delete(Object) - */ - public static final CascadingAction DELETE = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to delete: {0}", entityName ); - session.delete( entityName, child, isCascadeDeleteEnabled, ( Set ) anything ); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // delete does cascade to uninitialized collections - return CascadingAction.getAllElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - // orphans should be deleted during delete - return true; - } - @Override - public String toString() { - return "ACTION_DELETE"; - } - }; - - /** - * @see org.hibernate.Session#lock(Object, LockMode) - */ - public static final CascadingAction LOCK = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to lock: {0}", entityName ); - LockMode lockMode = LockMode.NONE; - LockOptions lr = new LockOptions(); - if ( anything instanceof LockOptions) { - LockOptions lockOptions = (LockOptions)anything; - lr.setTimeOut(lockOptions.getTimeOut()); - lr.setScope( lockOptions.getScope()); - if ( lockOptions.getScope() == true ) // cascade specified lockMode - lockMode = lockOptions.getLockMode(); - } - lr.setLockMode(lockMode); - session.buildLockRequest(lr).lock(entityName, child); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // lock doesn't cascade to uninitialized collections - return getLoadedElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - //TODO: should orphans really be deleted during lock??? - return false; - } - @Override - public String toString() { - return "ACTION_LOCK"; - } - }; - - /** - * @see org.hibernate.Session#refresh(Object) - */ - public static final CascadingAction REFRESH = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to refresh: {0}", entityName ); - session.refresh( child, (Map) anything ); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // refresh doesn't cascade to uninitialized collections - return getLoadedElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - return false; - } - @Override - public String toString() { - return "ACTION_REFRESH"; - } - }; - - /** - * @see org.hibernate.Session#evict(Object) - */ - public static final CascadingAction EVICT = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to evict: {0}", entityName ); - session.evict(child); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // evicts don't cascade to uninitialized collections - return getLoadedElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - return false; - } - @Override - public boolean performOnLazyProperty() { - return false; - } - @Override - public String toString() { - return "ACTION_EVICT"; - } - }; - - /** - * @see org.hibernate.Session#saveOrUpdate(Object) - */ - public static final CascadingAction SAVE_UPDATE = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to save or update: {0}", entityName ); - session.saveOrUpdate(entityName, child); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // saves / updates don't cascade to uninitialized collections - return getLoadedElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - // orphans should be deleted during save/update - return true; - } - @Override - public boolean performOnLazyProperty() { - return false; - } - @Override - public String toString() { - return "ACTION_SAVE_UPDATE"; - } - }; - - /** - * @see org.hibernate.Session#merge(Object) - */ - public static final CascadingAction MERGE = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to merge: {0}", entityName ); - session.merge( entityName, child, (Map) anything ); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // merges don't cascade to uninitialized collections -// //TODO: perhaps this does need to cascade after all.... - return getLoadedElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - // orphans should not be deleted during merge?? - return false; - } - @Override - public String toString() { - return "ACTION_MERGE"; - } - }; - - /** - * @see org.hibernate.Session#persist(Object) - */ - public static final CascadingAction PERSIST = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to persist: {0}" + entityName ); - session.persist( entityName, child, (Map) anything ); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // persists don't cascade to uninitialized collections - return CascadingAction.getAllElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - return false; - } - @Override - public boolean performOnLazyProperty() { - return false; - } - @Override - public String toString() { - return "ACTION_PERSIST"; - } - }; - - /** - * Execute persist during flush time - * - * @see org.hibernate.Session#persist(Object) - */ - public static final CascadingAction PERSIST_ON_FLUSH = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to persist on flush: {0}", entityName ); - session.persistOnFlush( entityName, child, (Map) anything ); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // persists don't cascade to uninitialized collections - return CascadingAction.getLoadedElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - return true; - } - @Override - public boolean requiresNoCascadeChecking() { - return true; - } - @Override - public void noCascade( - EventSource session, - Object child, - Object parent, - EntityPersister persister, - int propertyIndex) { - if ( child == null ) { - return; - } - Type type = persister.getPropertyTypes()[propertyIndex]; - if ( type.isEntityType() ) { - String childEntityName = ( ( EntityType ) type ).getAssociatedEntityName( session.getFactory() ); - - if ( ! isInManagedState( child, session ) - && ! ( child instanceof HibernateProxy ) //a proxy cannot be transient and it breaks ForeignKeys.isTransient - && ForeignKeys.isTransient( childEntityName, child, null, session ) ) { - String parentEntiytName = persister.getEntityName(); - String propertyName = persister.getPropertyNames()[propertyIndex]; - throw new TransientPropertyValueException( - "object references an unsaved transient instance - save the transient instance before flushing", - childEntityName, - parentEntiytName, - propertyName - ); - - } - } - } - @Override - public boolean performOnLazyProperty() { - return false; - } - - private boolean isInManagedState(Object child, EventSource session) { - EntityEntry entry = session.getPersistenceContext().getEntry( child ); - return entry != null && - ( - entry.getStatus() == Status.MANAGED || - entry.getStatus() == Status.READ_ONLY || - entry.getStatus() == Status.SAVING - ); - } - - @Override - public String toString() { - return "ACTION_PERSIST_ON_FLUSH"; - } - }; - - /** - * @see org.hibernate.Session#replicate(Object, org.hibernate.ReplicationMode) - */ - public static final CascadingAction REPLICATE = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.tracev( "Cascading to replicate: {0}", entityName ); - session.replicate( entityName, child, (ReplicationMode) anything ); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // replicate does cascade to uninitialized collections - return getLoadedElementsIterator(session, collectionType, collection); - } - @Override - public boolean deleteOrphans() { - return false; //I suppose? - } - @Override - public String toString() { - return "ACTION_REPLICATE"; - } - }; - - - // static helper methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - /** - * Given a collection, get an iterator of all its children, loading them - * from the database if necessary. - * - * @param session The session within which the cascade is occuring. - * @param collectionType The mapping type of the collection. - * @param collection The collection instance. - * @return The children iterator. - */ - private static Iterator getAllElementsIterator( - EventSource session, - CollectionType collectionType, - Object collection) { - return collectionType.getElementsIterator( collection, session ); - } - - /** - * Iterate just the elements of the collection that are already there. Don't load - * any new elements from the database. - */ - public static Iterator getLoadedElementsIterator(SessionImplementor session, CollectionType collectionType, Object collection) { - if ( collectionIsInitialized(collection) ) { - // handles arrays and newly instantiated collections - return collectionType.getElementsIterator(collection, session); - } - else { - // does not handle arrays (thats ok, cos they can't be lazy) - // or newly instantiated collections, so we can do the cast - return ( (PersistentCollection) collection ).queuedAdditionIterator(); - } - } - - private static boolean collectionIsInitialized(Object collection) { - return !(collection instanceof PersistentCollection) || ( (PersistentCollection) collection ).wasInitialized(); - } + public boolean performOnLazyProperty(); } \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java new file mode 100644 index 0000000000..a39014bb3d --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java @@ -0,0 +1,521 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, 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.engine.spi; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.jboss.logging.Logger; + +import org.hibernate.HibernateException; +import org.hibernate.LockMode; +import org.hibernate.LockOptions; +import org.hibernate.ReplicationMode; +import org.hibernate.TransientPropertyValueException; +import org.hibernate.collection.spi.PersistentCollection; +import org.hibernate.engine.internal.ForeignKeys; +import org.hibernate.event.spi.EventSource; +import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.proxy.HibernateProxy; +import org.hibernate.type.CollectionType; +import org.hibernate.type.EntityType; +import org.hibernate.type.Type; + +/** + * @author Steve Ebersole + */ +public class CascadingActions { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, + CascadingAction.class.getName() + ); + + /** + * Disallow instantiation + */ + private CascadingActions() { + } + + /** + * @see org.hibernate.Session#delete(Object) + */ + public static final CascadingAction DELETE = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) { + LOG.tracev( "Cascading to delete: {0}", entityName ); + session.delete( entityName, child, isCascadeDeleteEnabled, (Set) anything ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // delete does cascade to uninitialized collections + return getAllElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + // orphans should be deleted during delete + return true; + } + + @Override + public String toString() { + return "ACTION_DELETE"; + } + }; + + /** + * @see org.hibernate.Session#lock(Object, org.hibernate.LockMode) + */ + public static final CascadingAction LOCK = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) { + LOG.tracev( "Cascading to lock: {0}", entityName ); + LockMode lockMode = LockMode.NONE; + LockOptions lr = new LockOptions(); + if ( anything instanceof LockOptions ) { + LockOptions lockOptions = (LockOptions) anything; + lr.setTimeOut( lockOptions.getTimeOut() ); + lr.setScope( lockOptions.getScope() ); + if ( lockOptions.getScope() ) { + lockMode = lockOptions.getLockMode(); + } + } + lr.setLockMode( lockMode ); + session.buildLockRequest( lr ).lock( entityName, child ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // lock doesn't cascade to uninitialized collections + return getLoadedElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + //TODO: should orphans really be deleted during lock??? + return false; + } + + @Override + public String toString() { + return "ACTION_LOCK"; + } + }; + + /** + * @see org.hibernate.Session#refresh(Object) + */ + public static final CascadingAction REFRESH = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) + throws HibernateException { + LOG.tracev( "Cascading to refresh: {0}", entityName ); + session.refresh( child, (Map) anything ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // refresh doesn't cascade to uninitialized collections + return getLoadedElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + return false; + } + + @Override + public String toString() { + return "ACTION_REFRESH"; + } + }; + + /** + * @see org.hibernate.Session#evict(Object) + */ + public static final CascadingAction EVICT = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) + throws HibernateException { + LOG.tracev( "Cascading to evict: {0}", entityName ); + session.evict( child ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // evicts don't cascade to uninitialized collections + return getLoadedElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + return false; + } + + @Override + public boolean performOnLazyProperty() { + return false; + } + + @Override + public String toString() { + return "ACTION_EVICT"; + } + }; + + /** + * @see org.hibernate.Session#saveOrUpdate(Object) + */ + public static final CascadingAction SAVE_UPDATE = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) + throws HibernateException { + LOG.tracev( "Cascading to save or update: {0}", entityName ); + session.saveOrUpdate( entityName, child ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // saves / updates don't cascade to uninitialized collections + return getLoadedElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + // orphans should be deleted during save/update + return true; + } + + @Override + public boolean performOnLazyProperty() { + return false; + } + + @Override + public String toString() { + return "ACTION_SAVE_UPDATE"; + } + }; + + /** + * @see org.hibernate.Session#merge(Object) + */ + public static final CascadingAction MERGE = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) + throws HibernateException { + LOG.tracev( "Cascading to merge: {0}", entityName ); + session.merge( entityName, child, (Map) anything ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // merges don't cascade to uninitialized collections + return getLoadedElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + // orphans should not be deleted during merge?? + return false; + } + + @Override + public String toString() { + return "ACTION_MERGE"; + } + }; + + /** + * @see org.hibernate.Session#persist(Object) + */ + public static final CascadingAction PERSIST = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) + throws HibernateException { + LOG.tracev( "Cascading to persist: {0}" + entityName ); + session.persist( entityName, child, (Map) anything ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // persists don't cascade to uninitialized collections + return getAllElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + return false; + } + + @Override + public boolean performOnLazyProperty() { + return false; + } + + @Override + public String toString() { + return "ACTION_PERSIST"; + } + }; + + /** + * Execute persist during flush time + * + * @see org.hibernate.Session#persist(Object) + */ + public static final CascadingAction PERSIST_ON_FLUSH = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) + throws HibernateException { + LOG.tracev( "Cascading to persist on flush: {0}", entityName ); + session.persistOnFlush( entityName, child, (Map) anything ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // persists don't cascade to uninitialized collections + return getLoadedElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + return true; + } + + @Override + public boolean requiresNoCascadeChecking() { + return true; + } + + @Override + public void noCascade( + EventSource session, + Object child, + Object parent, + EntityPersister persister, + int propertyIndex) { + if ( child == null ) { + return; + } + Type type = persister.getPropertyTypes()[propertyIndex]; + if ( type.isEntityType() ) { + String childEntityName = ((EntityType) type).getAssociatedEntityName( session.getFactory() ); + + if ( !isInManagedState( child, session ) + && !(child instanceof HibernateProxy) //a proxy cannot be transient and it breaks ForeignKeys.isTransient + && ForeignKeys.isTransient( childEntityName, child, null, session ) ) { + String parentEntiytName = persister.getEntityName(); + String propertyName = persister.getPropertyNames()[propertyIndex]; + throw new TransientPropertyValueException( + "object references an unsaved transient instance - save the transient instance before flushing", + childEntityName, + parentEntiytName, + propertyName + ); + + } + } + } + + @Override + public boolean performOnLazyProperty() { + return false; + } + + private boolean isInManagedState(Object child, EventSource session) { + EntityEntry entry = session.getPersistenceContext().getEntry( child ); + return entry != null && + ( + entry.getStatus() == Status.MANAGED || + entry.getStatus() == Status.READ_ONLY || + entry.getStatus() == Status.SAVING + ); + } + + @Override + public String toString() { + return "ACTION_PERSIST_ON_FLUSH"; + } + }; + + /** + * @see org.hibernate.Session#replicate + */ + public static final CascadingAction REPLICATE = new BaseCascadingAction() { + @Override + public void cascade( + EventSource session, + Object child, + String entityName, + Object anything, + boolean isCascadeDeleteEnabled) + throws HibernateException { + LOG.tracev( "Cascading to replicate: {0}", entityName ); + session.replicate( entityName, child, (ReplicationMode) anything ); + } + + @Override + public Iterator getCascadableChildrenIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + // replicate does cascade to uninitialized collections + return getLoadedElementsIterator( session, collectionType, collection ); + } + + @Override + public boolean deleteOrphans() { + return false; //I suppose? + } + + @Override + public String toString() { + return "ACTION_REPLICATE"; + } + }; + + public abstract static class BaseCascadingAction implements CascadingAction { + @Override + public boolean requiresNoCascadeChecking() { + return false; + } + + @Override + public void noCascade(EventSource session, Object child, Object parent, EntityPersister persister, int propertyIndex) { + } + + @Override + public boolean performOnLazyProperty() { + return true; + } + } + + /** + * Given a collection, get an iterator of all its children, loading them + * from the database if necessary. + * + * @param session The session within which the cascade is occuring. + * @param collectionType The mapping type of the collection. + * @param collection The collection instance. + * + * @return The children iterator. + */ + private static Iterator getAllElementsIterator( + EventSource session, + CollectionType collectionType, + Object collection) { + return collectionType.getElementsIterator( collection, session ); + } + + /** + * Iterate just the elements of the collection that are already there. Don't load + * any new elements from the database. + */ + public static Iterator getLoadedElementsIterator( + SessionImplementor session, + CollectionType collectionType, + Object collection) { + if ( collectionIsInitialized( collection ) ) { + // handles arrays and newly instantiated collections + return collectionType.getElementsIterator( collection, session ); + } + else { + // does not handle arrays (thats ok, cos they can't be lazy) + // or newly instantiated collections, so we can do the cast + return ((PersistentCollection) collection).queuedAdditionIterator(); + } + } + + private static boolean collectionIsInitialized(Object collection) { + return !(collection instanceof PersistentCollection) || ((PersistentCollection) collection).wasInitialized(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java index cd4155f5b7..b64cd9a7ae 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java @@ -37,6 +37,7 @@ import org.hibernate.engine.internal.Cascade; import org.hibernate.engine.internal.Collections; import org.hibernate.engine.spi.ActionQueue; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.CollectionEntry; import org.hibernate.engine.spi.CollectionKey; import org.hibernate.engine.spi.EntityEntry; @@ -168,7 +169,7 @@ public abstract class AbstractFlushingEventListener implements Serializable { protected Object getAnything() { return null; } protected CascadingAction getCascadingAction() { - return CascadingAction.SAVE_UPDATE; + return CascadingActions.SAVE_UPDATE; } /** diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java index 491b0820e0..e993d6b1cf 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java @@ -37,7 +37,7 @@ import org.hibernate.classic.Lifecycle; import org.hibernate.engine.internal.Cascade; import org.hibernate.engine.internal.ForeignKeys; import org.hibernate.engine.internal.Nullability; -import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; @@ -321,7 +321,7 @@ public class DefaultDeleteEventListener implements DeleteEventListener { session.getPersistenceContext().incrementCascadeLevel(); try { // cascade-delete to collections BEFORE the collection owner is deleted - new Cascade( CascadingAction.DELETE, Cascade.AFTER_INSERT_BEFORE_DELETE, session ) + new Cascade( CascadingActions.DELETE, Cascade.AFTER_INSERT_BEFORE_DELETE, session ) .cascade( persister, entity, transientEntities ); } finally { @@ -341,7 +341,7 @@ public class DefaultDeleteEventListener implements DeleteEventListener { session.getPersistenceContext().incrementCascadeLevel(); try { // cascade-delete to many-to-one AFTER the parent was deleted - new Cascade( CascadingAction.DELETE, Cascade.BEFORE_INSERT_AFTER_DELETE, session ) + new Cascade( CascadingActions.DELETE, Cascade.BEFORE_INSERT_AFTER_DELETE, session ) .cascade( persister, entity, transientEntities ); } finally { diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java index d6970ad9b3..8bf320502b 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java @@ -29,7 +29,7 @@ import org.jboss.logging.Logger; import org.hibernate.HibernateException; import org.hibernate.engine.internal.Cascade; -import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; @@ -117,7 +117,7 @@ public class DefaultEvictEventListener implements EvictEventListener { // This is now handled by removeEntity() //session.getPersistenceContext().removeDatabaseSnapshot(key); - new Cascade( CascadingAction.EVICT, Cascade.AFTER_EVICT, session ) + new Cascade( CascadingActions.EVICT, Cascade.AFTER_EVICT, session ) .cascade( persister, object ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java index 5e42c38973..3c2ca3e280 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java @@ -30,7 +30,7 @@ import org.hibernate.LockMode; import org.hibernate.TransientObjectException; import org.hibernate.engine.internal.Cascade; import org.hibernate.engine.internal.ForeignKeys; -import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.event.spi.EventSource; @@ -89,7 +89,7 @@ public class DefaultLockEventListener extends AbstractLockUpgradeEventListener i EventSource source = event.getSession(); source.getPersistenceContext().incrementCascadeLevel(); try { - new Cascade(CascadingAction.LOCK, Cascade.AFTER_LOCK, source) + new Cascade( CascadingActions.LOCK, Cascade.AFTER_LOCK, source) .cascade( persister, entity, event.getLockOptions() ); } finally { diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java index e9ee3a13fe..5d00d6afad 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java @@ -36,6 +36,7 @@ import org.hibernate.WrongClassException; import org.hibernate.bytecode.instrumentation.spi.FieldInterceptor; import org.hibernate.engine.internal.Cascade; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.SessionImplementor; @@ -447,7 +448,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme @Override protected CascadingAction getCascadeAction() { - return CascadingAction.MERGE; + return CascadingActions.MERGE; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java index b68b69c105..69216478fc 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistEventListener.java @@ -32,6 +32,7 @@ import org.hibernate.HibernateException; import org.hibernate.ObjectDeletedException; import org.hibernate.PersistentObjectException; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.Status; @@ -60,7 +61,7 @@ public class DefaultPersistEventListener extends AbstractSaveEventListener imple @Override protected CascadingAction getCascadeAction() { - return CascadingAction.PERSIST; + return CascadingActions.PERSIST; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistOnFlushEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistOnFlushEventListener.java index 3f8095688e..58f7375b6d 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistOnFlushEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultPersistOnFlushEventListener.java @@ -24,6 +24,7 @@ package org.hibernate.event.internal; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; /** * When persist is used as the cascade action, persistOnFlush should be used @@ -31,6 +32,6 @@ import org.hibernate.engine.spi.CascadingAction; */ public class DefaultPersistOnFlushEventListener extends DefaultPersistEventListener { protected CascadingAction getCascadeAction() { - return CascadingAction.PERSIST_ON_FLUSH; + return CascadingActions.PERSIST_ON_FLUSH; } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java index 3f6c2c524a..d37818046d 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java @@ -34,7 +34,7 @@ import org.hibernate.PersistentObjectException; import org.hibernate.UnresolvableObjectException; import org.hibernate.cache.spi.CacheKey; import org.hibernate.engine.internal.Cascade; -import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -119,7 +119,7 @@ public class DefaultRefreshEventListener implements RefreshEventListener { // cascade the refresh prior to refreshing this entity refreshedAlready.put(object, object); - new Cascade( CascadingAction.REFRESH, Cascade.BEFORE_REFRESH, source) + new Cascade( CascadingActions.REFRESH, Cascade.BEFORE_REFRESH, source) .cascade( persister, object, refreshedAlready ); if ( e != null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java index e319792cf6..7885ea6c4c 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java @@ -33,6 +33,7 @@ import org.hibernate.ReplicationMode; import org.hibernate.TransientObjectException; import org.hibernate.engine.internal.Cascade; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.Status; @@ -207,7 +208,7 @@ public class DefaultReplicateEventListener extends AbstractSaveEventListener imp EventSource source) { source.getPersistenceContext().incrementCascadeLevel(); try { - new Cascade( CascadingAction.REPLICATE, Cascade.AFTER_UPDATE, source ) + new Cascade( CascadingActions.REPLICATE, Cascade.AFTER_UPDATE, source ) .cascade( persister, entity, replicationMode ); } finally { @@ -217,6 +218,6 @@ public class DefaultReplicateEventListener extends AbstractSaveEventListener imp @Override protected CascadingAction getCascadeAction() { - return CascadingAction.REPLICATE; + return CascadingActions.REPLICATE; } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java index 1e8276f7c8..8fe131bc3d 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultSaveOrUpdateEventListener.java @@ -35,6 +35,7 @@ import org.hibernate.TransientObjectException; import org.hibernate.classic.Lifecycle; import org.hibernate.engine.internal.Cascade; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -358,7 +359,7 @@ public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener EventSource source = event.getSession(); source.getPersistenceContext().incrementCascadeLevel(); try { - new Cascade( CascadingAction.SAVE_UPDATE, Cascade.AFTER_UPDATE, source ) + new Cascade( CascadingActions.SAVE_UPDATE, Cascade.AFTER_UPDATE, source ) .cascade( persister, entity ); } finally { @@ -368,6 +369,6 @@ public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener @Override protected CascadingAction getCascadeAction() { - return CascadingAction.SAVE_UPDATE; + return CascadingActions.SAVE_UPDATE; } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Property.java b/hibernate-core/src/main/java/org/hibernate/mapping/Property.java index cffdc68e2d..9b4af9da92 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Property.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Property.java @@ -31,6 +31,7 @@ import org.hibernate.EntityMode; import org.hibernate.MappingException; import org.hibernate.PropertyNotFoundException; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.engine.spi.Mapping; import org.hibernate.internal.util.collections.ArrayHelper; import org.hibernate.property.Getter; @@ -124,8 +125,8 @@ public class Property implements Serializable, MetaAttributable { } int length = compositeType.getSubtypes().length; for ( int i=0; i cascadeStyles) { List cascadeStyleList = new ArrayList(); for ( CascadeStyle style : cascadeStyles ) { - if ( style != CascadeStyle.NONE ) { + if ( style != CascadeStyles.NONE ) { cascadeStyleList.add( style ); } - if ( style == CascadeStyle.DELETE_ORPHAN || - style == CascadeStyle.ALL_DELETE_ORPHAN ) { + if ( style == CascadeStyles.DELETE_ORPHAN || + style == CascadeStyles.ALL_DELETE_ORPHAN ) { orphanDelete = true; } } if ( cascadeStyleList.isEmpty() ) { - cascadeStyle = CascadeStyle.NONE; + cascadeStyle = CascadeStyles.NONE; } else if ( cascadeStyleList.size() == 1 ) { cascadeStyle = cascadeStyleList.get( 0 ); } else { - cascadeStyle = new CascadeStyle.MultipleCascadeStyle( + cascadeStyle = new CascadeStyles.MultipleCascadeStyle( cascadeStyleList.toArray( new CascadeStyle[ cascadeStyleList.size() ] ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CascadeType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CascadeType.java index 1b414b4d6c..14a4f2aa0a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CascadeType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CascadeType.java @@ -28,6 +28,7 @@ import java.util.Map; import org.hibernate.MappingException; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; /** @@ -125,18 +126,18 @@ public enum CascadeType { private static final Map cascadeTypeToCascadeStyle = new HashMap(); static { - cascadeTypeToCascadeStyle.put( ALL, CascadeStyle.ALL ); - cascadeTypeToCascadeStyle.put( ALL_DELETE_ORPHAN, CascadeStyle.ALL_DELETE_ORPHAN ); - cascadeTypeToCascadeStyle.put( UPDATE, CascadeStyle.UPDATE ); - cascadeTypeToCascadeStyle.put( PERSIST, CascadeStyle.PERSIST ); - cascadeTypeToCascadeStyle.put( MERGE, CascadeStyle.MERGE ); - cascadeTypeToCascadeStyle.put( LOCK, CascadeStyle.LOCK ); - cascadeTypeToCascadeStyle.put( REFRESH, CascadeStyle.REFRESH ); - cascadeTypeToCascadeStyle.put( REPLICATE, CascadeStyle.REPLICATE ); - cascadeTypeToCascadeStyle.put( EVICT, CascadeStyle.EVICT ); - cascadeTypeToCascadeStyle.put( DELETE, CascadeStyle.DELETE ); - cascadeTypeToCascadeStyle.put( DELETE_ORPHAN, CascadeStyle.DELETE_ORPHAN ); - cascadeTypeToCascadeStyle.put( NONE, CascadeStyle.NONE ); + cascadeTypeToCascadeStyle.put( ALL, CascadeStyles.ALL ); + cascadeTypeToCascadeStyle.put( ALL_DELETE_ORPHAN, CascadeStyles.ALL_DELETE_ORPHAN ); + cascadeTypeToCascadeStyle.put( UPDATE, CascadeStyles.UPDATE ); + cascadeTypeToCascadeStyle.put( PERSIST, CascadeStyles.PERSIST ); + cascadeTypeToCascadeStyle.put( MERGE, CascadeStyles.MERGE ); + cascadeTypeToCascadeStyle.put( LOCK, CascadeStyles.LOCK ); + cascadeTypeToCascadeStyle.put( REFRESH, CascadeStyles.REFRESH ); + cascadeTypeToCascadeStyle.put( REPLICATE, CascadeStyles.REPLICATE ); + cascadeTypeToCascadeStyle.put( EVICT, CascadeStyles.EVICT ); + cascadeTypeToCascadeStyle.put( DELETE, CascadeStyles.DELETE ); + cascadeTypeToCascadeStyle.put( DELETE_ORPHAN, CascadeStyles.DELETE_ORPHAN ); + cascadeTypeToCascadeStyle.put( NONE, CascadeStyles.NONE ); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java index 93876ba1bd..798ff70184 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java @@ -31,6 +31,7 @@ import org.hibernate.FetchMode; import org.hibernate.engine.FetchStyle; import org.hibernate.engine.FetchTiming; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.metamodel.domain.SingularAttribute; /** @@ -94,18 +95,18 @@ public class ManyToOneAttributeBinding extends BasicAttributeBinding implements public void setCascadeStyles(Iterable cascadeStyles) { List cascadeStyleList = new ArrayList(); for ( CascadeStyle style : cascadeStyles ) { - if ( style != CascadeStyle.NONE ) { + if ( style != CascadeStyles.NONE ) { cascadeStyleList.add( style ); } } if ( cascadeStyleList.isEmpty() ) { - cascadeStyle = CascadeStyle.NONE; + cascadeStyle = CascadeStyles.NONE; } else if ( cascadeStyleList.size() == 1 ) { cascadeStyle = cascadeStyleList.get( 0 ); } else { - cascadeStyle = new CascadeStyle.MultipleCascadeStyle( + cascadeStyle = new CascadeStyles.MultipleCascadeStyle( cascadeStyleList.toArray( new CascadeStyle[ cascadeStyleList.size() ] ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EnumConversionHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EnumConversionHelper.java index b8f8b7da4d..98ae17a720 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EnumConversionHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EnumConversionHelper.java @@ -32,6 +32,7 @@ import javax.persistence.GenerationType; import org.hibernate.AssertionFailure; import org.hibernate.FetchMode; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.id.MultipleHiLoPerTableGenerator; import org.hibernate.internal.util.collections.CollectionHelper; @@ -67,22 +68,22 @@ public class EnumConversionHelper { public static CascadeStyle cascadeTypeToCascadeStyle(CascadeType cascadeType) { switch ( cascadeType ) { case ALL: { - return CascadeStyle.ALL; + return CascadeStyles.ALL; } case PERSIST: { - return CascadeStyle.PERSIST; + return CascadeStyles.PERSIST; } case MERGE: { - return CascadeStyle.MERGE; + return CascadeStyles.MERGE; } case REMOVE: { - return CascadeStyle.DELETE; + return CascadeStyles.DELETE; } case REFRESH: { - return CascadeStyle.REFRESH; + return CascadeStyles.REFRESH; } case DETACH: { - return CascadeStyle.EVICT; + return CascadeStyles.EVICT; } default: { throw new AssertionFailure( "Unknown cascade type" ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/Helper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/Helper.java index 71dd55af9d..a9c09233e4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/Helper.java @@ -32,6 +32,7 @@ import java.util.Set; import org.hibernate.MappingException; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement; import org.hibernate.internal.jaxb.mapping.hbm.EntityElement; @@ -189,7 +190,7 @@ public class Helper { cascades = bindingContext.getMappingDefaults().getCascadeStyle(); } for ( String cascade : StringHelper.split( ",", cascades ) ) { - cascadeStyles.add( CascadeStyle.getCascadeStyle( cascade ) ); + cascadeStyles.add( CascadeStyles.getCascadeStyle( cascade ) ); } return cascadeStyles; } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 36dc800e8e..fddd909167 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -44,7 +44,6 @@ import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.MappingException; -import org.hibernate.PropertyAccessException; import org.hibernate.QueryException; import org.hibernate.StaleObjectStateException; import org.hibernate.StaleStateException; @@ -65,7 +64,8 @@ import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey; import org.hibernate.engine.spi.CachedNaturalIdValueSource; import org.hibernate.engine.spi.CascadeStyle; -import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadeStyles; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; @@ -1054,7 +1054,7 @@ public abstract class AbstractEntityPersister joinedFetchesList.add( associationAttributeBinding.getFetchMode() ); } else { - cascades.add( CascadeStyle.NONE ); + cascades.add( CascadeStyles.NONE ); joinedFetchesList.add( FetchMode.SELECT ); } } @@ -3784,11 +3784,11 @@ public abstract class AbstractEntityPersister loaders.put( "merge", - new CascadeEntityLoader( this, CascadingAction.MERGE, getFactory() ) + new CascadeEntityLoader( this, CascadingActions.MERGE, getFactory() ) ); loaders.put( "refresh", - new CascadeEntityLoader( this, CascadingAction.REFRESH, getFactory() ) + new CascadeEntityLoader( this, CascadingActions.REFRESH, getFactory() ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/PropertyFactory.java b/hibernate-core/src/main/java/org/hibernate/tuple/PropertyFactory.java index e4c6d2cf3f..8c1a51f931 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/PropertyFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/PropertyFactory.java @@ -29,6 +29,7 @@ import org.hibernate.EntityMode; import org.hibernate.FetchMode; import org.hibernate.engine.internal.UnsavedValueFactory; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.engine.spi.IdentifierValue; import org.hibernate.engine.spi.VersionValue; import org.hibernate.id.IdentifierGenerator; @@ -207,7 +208,7 @@ public class PropertyFactory { final CascadeStyle cascadeStyle = property.isAssociation() ? ( (AssociationAttributeBinding) property ).getCascadeStyle() - : CascadeStyle.NONE; + : CascadeStyles.NONE; return new VersionProperty( property.getAttribute().getName(), @@ -291,7 +292,7 @@ public class PropertyFactory { final SingularAttributeBinding singularAttributeBinding = ( SingularAttributeBinding ) property; final CascadeStyle cascadeStyle = singularAttributeBinding.isAssociation() ? ( (AssociationAttributeBinding) singularAttributeBinding ).getCascadeStyle() - : CascadeStyle.NONE; + : CascadeStyles.NONE; final FetchMode fetchMode = singularAttributeBinding.isAssociation() ? ( (AssociationAttributeBinding) singularAttributeBinding ).getFetchMode() : FetchMode.DEFAULT; @@ -317,7 +318,7 @@ public class PropertyFactory { final AbstractPluralAttributeBinding pluralAttributeBinding = (AbstractPluralAttributeBinding) property; final CascadeStyle cascadeStyle = pluralAttributeBinding.isAssociation() ? pluralAttributeBinding.getCascadeStyle() - : CascadeStyle.NONE; + : CascadeStyles.NONE; final FetchMode fetchMode = pluralAttributeBinding.isAssociation() ? pluralAttributeBinding.getFetchMode() : FetchMode.DEFAULT; diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java b/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java index 82742db83d..5db0051cc6 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java @@ -42,6 +42,7 @@ import org.hibernate.cfg.Environment; import org.hibernate.engine.OptimisticLockStyle; import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.ValueInclusion; import org.hibernate.internal.CoreMessageLogger; @@ -231,7 +232,7 @@ public class EntityMetamodel implements Serializable { hasLazy = true; } - if ( properties[i].getCascadeStyle() != CascadeStyle.NONE ) { + if ( properties[i].getCascadeStyle() != CascadeStyles.NONE ) { foundCascade = true; } @@ -478,7 +479,7 @@ public class EntityMetamodel implements Serializable { hasLazy = true; } - if ( properties[i].getCascadeStyle() != CascadeStyle.NONE ) { + if ( properties[i].getCascadeStyle() != CascadeStyles.NONE ) { foundCascade = true; } diff --git a/hibernate-core/src/main/java/org/hibernate/type/AnyType.java b/hibernate-core/src/main/java/org/hibernate/type/AnyType.java index 547d10a120..33a9bec9e0 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/AnyType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/AnyType.java @@ -40,6 +40,7 @@ import org.hibernate.MappingException; import org.hibernate.TransientObjectException; import org.hibernate.engine.internal.ForeignKeys; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.engine.spi.Mapping; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; @@ -270,7 +271,7 @@ public class AnyType extends AbstractType implements CompositeType, AssociationT } } public CascadeStyle getCascadeStyle(int i) { - return CascadeStyle.NONE; + return CascadeStyles.NONE; } public FetchMode getFetchMode(int i) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/CompositeCustomType.java b/hibernate-core/src/main/java/org/hibernate/type/CompositeCustomType.java index 9a4c668044..fe00857f20 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/CompositeCustomType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/CompositeCustomType.java @@ -38,6 +38,7 @@ import org.hibernate.FetchMode; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.engine.spi.CascadeStyle; +import org.hibernate.engine.spi.CascadeStyles; import org.hibernate.engine.spi.Mapping; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; @@ -121,7 +122,7 @@ public class CompositeCustomType extends AbstractType implements CompositeType, } public CascadeStyle getCascadeStyle(int i) { - return CascadeStyle.NONE; + return CascadeStyles.NONE; } public FetchMode getFetchMode(int i) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java index 8c8e9b0c5e..40f8cb9eff 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/AbstractJPATest.java @@ -30,6 +30,7 @@ import javax.persistence.EntityNotFoundException; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.event.internal.DefaultAutoFlushEventListener; import org.hibernate.event.internal.DefaultFlushEntityEventListener; @@ -151,7 +152,7 @@ public abstract class AbstractJPATest extends BaseCoreFunctionalTestCase { public static class JPAPersistOnFlushEventListener extends JPAPersistEventListener { @Override protected CascadingAction getCascadeAction() { - return CascadingAction.PERSIST_ON_FLUSH; + return CascadingActions.PERSIST_ON_FLUSH; } } @@ -161,7 +162,7 @@ public abstract class AbstractJPATest extends BaseCoreFunctionalTestCase { @Override protected CascadingAction getCascadingAction() { - return CascadingAction.PERSIST_ON_FLUSH; + return CascadingActions.PERSIST_ON_FLUSH; } @Override @@ -176,7 +177,7 @@ public abstract class AbstractJPATest extends BaseCoreFunctionalTestCase { @Override protected CascadingAction getCascadingAction() { - return CascadingAction.PERSIST_ON_FLUSH; + return CascadingActions.PERSIST_ON_FLUSH; } @Override diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/engine/spi/JpaCascadeStyle.java b/hibernate-entitymanager/src/main/java/org/hibernate/engine/spi/JpaCascadeStyle.java deleted file mode 100644 index 2b1b515161..0000000000 --- a/hibernate-entitymanager/src/main/java/org/hibernate/engine/spi/JpaCascadeStyle.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2009-2012, 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.engine.spi; - -/** - * Because CascadeStyle is not opened and package protected, - * I need to subclass and override the persist alias - * - * Note that This class has to be triggered by JpaPersistEventListener at class loading time - * - * TODO get rid of it for 3.3 - * - * @author Emmanuel Bernard - */ -public abstract class JpaCascadeStyle extends CascadeStyle { - - /** - * cascade using JpaCascadingAction - */ - public static final CascadeStyle PERSIST_JPA = new CascadeStyle() { - public boolean doCascade(CascadingAction action) { - return action== JpaCascadingAction.PERSIST_SKIPLAZY - || action==CascadingAction.PERSIST_ON_FLUSH; - } - public String toString() { - return "STYLE_PERSIST_SKIPLAZY"; - } - }; - - static { - STYLES.put( "persist", PERSIST_JPA ); - } -} diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/engine/spi/JpaCascadingAction.java b/hibernate-entitymanager/src/main/java/org/hibernate/engine/spi/JpaCascadingAction.java deleted file mode 100644 index 99da8ad2ba..0000000000 --- a/hibernate-entitymanager/src/main/java/org/hibernate/engine/spi/JpaCascadingAction.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2009, Red Hat Middleware LLC 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 Middleware LLC. - * - * 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.engine.spi; - -import java.util.Iterator; -import java.util.Map; - -import org.jboss.logging.Logger; - -import org.hibernate.HibernateException; -import org.hibernate.event.spi.EventSource; -import org.hibernate.type.CollectionType; - -/** - * Because of CascadingAction constructor visibility - * I need a packaged friendly subclass - * TODO Get rid of it for 3.3 - * @author Emmanuel Bernard - */ -public abstract class JpaCascadingAction extends CascadingAction { - private static final Logger LOG = Logger.getLogger( JpaCascadingAction.class.getName() ); - - /** - * @see org.hibernate.Session#persist(Object) - */ - public static final CascadingAction PERSIST_SKIPLAZY = new CascadingAction() { - @Override - public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) - throws HibernateException { - LOG.trace("Cascading to persist: " + entityName); - session.persist( entityName, child, (Map) anything ); - } - @Override - public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { - // persists don't cascade to uninitialized collections - return CascadingAction.getLoadedElementsIterator( session, collectionType, collection ); - } - @Override - public boolean deleteOrphans() { - return false; - } - @Override - public boolean performOnLazyProperty() { - return false; - } - @Override - public String toString() { - return "ACTION_PERSIST_SKIPLAZY"; - } - }; - -} diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaAutoFlushEventListener.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaAutoFlushEventListener.java index b55ca1a5e8..d95ce64369 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaAutoFlushEventListener.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaAutoFlushEventListener.java @@ -26,6 +26,7 @@ package org.hibernate.jpa.internal.event; import java.util.IdentityHashMap; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.event.internal.DefaultAutoFlushEventListener; import org.hibernate.event.spi.AutoFlushEventListener; @@ -43,7 +44,7 @@ public class JpaAutoFlushEventListener @Override protected CascadingAction getCascadingAction() { - return CascadingAction.PERSIST_ON_FLUSH; + return CascadingActions.PERSIST_ON_FLUSH; } @Override diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaFlushEventListener.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaFlushEventListener.java index 836489a259..4ebcdb5d35 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaFlushEventListener.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaFlushEventListener.java @@ -26,6 +26,7 @@ package org.hibernate.jpa.internal.event; import java.util.IdentityHashMap; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.event.internal.DefaultFlushEventListener; import org.hibernate.event.spi.FlushEventListener; @@ -40,7 +41,7 @@ public class JpaFlushEventListener extends DefaultFlushEventListener implements @Override protected CascadingAction getCascadingAction() { - return CascadingAction.PERSIST_ON_FLUSH; + return CascadingActions.PERSIST_ON_FLUSH; } @Override diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaIntegrator.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaIntegrator.java index cdc26a8780..b5ed16ad92 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaIntegrator.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaIntegrator.java @@ -31,13 +31,16 @@ import org.hibernate.MappingException; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.ejb.AvailableSettings; +import org.hibernate.engine.spi.CascadeStyles; +import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.event.service.spi.DuplicationStrategy; import org.hibernate.event.service.spi.EventListenerGroup; import org.hibernate.event.service.spi.EventListenerRegistry; import org.hibernate.event.spi.EventType; import org.hibernate.integrator.spi.Integrator; +import org.hibernate.jpa.AvailableSettings; import org.hibernate.mapping.PersistentClass; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.source.MetadataImplementor; @@ -89,6 +92,24 @@ public class JpaIntegrator implements Integrator { Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { + // first, register the JPA-specific persist cascade style + CascadeStyles.registerCascadeStyle( + "persist", + new CascadeStyles.BaseCascadeStyle() { + @Override + public boolean doCascade(CascadingAction action) { + return action == JpaPersistEventListener.PERSIST_SKIPLAZY + || action == CascadingActions.PERSIST_ON_FLUSH; + } + + @Override + public String toString() { + return "STYLE_PERSIST_SKIPLAZY"; + } + } + ); + + // then prepare listeners final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); boolean isSecurityEnabled = configuration.getProperties().containsKey( AvailableSettings.JACC_ENABLED ); @@ -233,7 +254,8 @@ public class JpaIntegrator implements Integrator { } try { callbackHandler.add(classLoaderSvc.classForName(name), classLoaderSvc, binding); - } catch (ClassLoadingException error) { + } + catch (ClassLoadingException error) { throw new MappingException( "entity class not found: " + name, error ); } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistEventListener.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistEventListener.java index 4f584d4a1b..ccb6235a99 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistEventListener.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistEventListener.java @@ -24,12 +24,17 @@ package org.hibernate.jpa.internal.event; import java.io.Serializable; +import java.util.Iterator; +import java.util.Map; +import org.jboss.logging.Logger; + +import org.hibernate.HibernateException; import org.hibernate.engine.spi.CascadingAction; -import org.hibernate.engine.spi.JpaCascadeStyle; -import org.hibernate.engine.spi.JpaCascadingAction; +import org.hibernate.engine.spi.CascadingActions; import org.hibernate.event.internal.DefaultPersistEventListener; import org.hibernate.event.spi.EventSource; +import org.hibernate.type.CollectionType; /** * Overrides the LifeCycle OnSave call to call the PrePersist operation @@ -37,9 +42,7 @@ import org.hibernate.event.spi.EventSource; * @author Emmanuel Bernard */ public class JpaPersistEventListener extends DefaultPersistEventListener implements CallbackHandlerConsumer { - static { - JpaCascadeStyle.PERSIST_JPA.hasOrphanDelete(); //triggers class loading to override persist with PERSIST_JPA - } + private static final Logger log = Logger.getLogger( JpaPersistEventListener.class ); private EntityCallbackHandler callbackHandler; @@ -81,6 +84,32 @@ public class JpaPersistEventListener extends DefaultPersistEventListener impleme @Override protected CascadingAction getCascadeAction() { - return JpaCascadingAction.PERSIST_SKIPLAZY; + return PERSIST_SKIPLAZY; } + + public static final CascadingAction PERSIST_SKIPLAZY = new CascadingActions.BaseCascadingAction() { + @Override + public void cascade(EventSource session, Object child, String entityName, Object anything, boolean isCascadeDeleteEnabled) + throws HibernateException { + log.trace( "Cascading persist to : " + entityName ); + session.persist( entityName, child, (Map) anything ); + } + @Override + public Iterator getCascadableChildrenIterator(EventSource session, CollectionType collectionType, Object collection) { + // persists don't cascade to uninitialized collections + return CascadingActions.getLoadedElementsIterator( session, collectionType, collection ); + } + @Override + public boolean deleteOrphans() { + return false; + } + @Override + public boolean performOnLazyProperty() { + return false; + } + @Override + public String toString() { + return "ACTION_PERSIST_SKIPLAZY"; + } + }; } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistOnFlushEventListener.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistOnFlushEventListener.java index 88a91ed4ce..132c4aedb9 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistOnFlushEventListener.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/event/JpaPersistOnFlushEventListener.java @@ -24,6 +24,7 @@ package org.hibernate.jpa.internal.event; import org.hibernate.engine.spi.CascadingAction; +import org.hibernate.engine.spi.CascadingActions; /** * @author Emmanuel Bernard @@ -31,6 +32,6 @@ import org.hibernate.engine.spi.CascadingAction; public class JpaPersistOnFlushEventListener extends JpaPersistEventListener { @Override protected CascadingAction getCascadeAction() { - return CascadingAction.PERSIST_ON_FLUSH; + return CascadingActions.PERSIST_ON_FLUSH; } }