Search in sources :

Example 1 with CascadeStyle

use of org.hibernate.engine.spi.CascadeStyle in project hibernate-orm by hibernate.

the class Cascade method cascadeComponent.

private static void cascadeComponent(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final int componentPathStackDepth, final Object parent, final Object child, final CompositeType componentType, final Object anything) {
    Object[] children = null;
    final Type[] types = componentType.getSubtypes();
    final String[] propertyNames = componentType.getPropertyNames();
    for (int i = 0; i < types.length; i++) {
        final CascadeStyle componentPropertyStyle = componentType.getCascadeStyle(i);
        final String subPropertyName = propertyNames[i];
        if (componentPropertyStyle.doCascade(action)) {
            if (children == null) {
                // Get children on demand.
                children = componentType.getPropertyValues(child, eventSource);
            }
            cascadeProperty(action, cascadePoint, eventSource, componentPathStackDepth + 1, parent, children[i], types[i], componentPropertyStyle, subPropertyName, anything, false);
        }
    }
}
Also used : CascadeStyle(org.hibernate.engine.spi.CascadeStyle) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type)

Example 2 with CascadeStyle

use of org.hibernate.engine.spi.CascadeStyle in project hibernate-orm by hibernate.

the class Property method getCascadeStyle.

private static CascadeStyle getCascadeStyle(String cascade) {
    if (cascade == null || cascade.equals("none")) {
        return CascadeStyles.NONE;
    } else {
        StringTokenizer tokens = new StringTokenizer(cascade, ", ");
        CascadeStyle[] styles = new CascadeStyle[tokens.countTokens()];
        int i = 0;
        while (tokens.hasMoreTokens()) {
            styles[i++] = CascadeStyles.getCascadeStyle(tokens.nextToken());
        }
        return new CascadeStyles.MultipleCascadeStyle(styles);
    }
}
Also used : CascadeStyle(org.hibernate.engine.spi.CascadeStyle) StringTokenizer(java.util.StringTokenizer)

Example 3 with CascadeStyle

use of org.hibernate.engine.spi.CascadeStyle in project hibernate-orm by hibernate.

the class Cascade method cascade.

/**
 * Cascade an action from the parent entity instance to all its children.  This
 * form is typically called from within cascade actions.
 *
 * @param persister The parent's entity persister
 * @param parent The parent reference.
 * @param anything Anything ;)   Typically some form of cascade-local cache
 * which is specific to each CascadingAction type
 */
public static void cascade(final CascadingAction action, final CascadePoint cascadePoint, final EventSource eventSource, final EntityPersister persister, final Object parent, final Object anything) throws HibernateException {
    if (persister.hasCascades() || action.requiresNoCascadeChecking()) {
        // performance opt
        final boolean traceEnabled = LOG.isTraceEnabled();
        if (traceEnabled) {
            LOG.tracev("Processing cascade {0} for: {1}", action, persister.getEntityName());
        }
        final Type[] types = persister.getPropertyTypes();
        final String[] propertyNames = persister.getPropertyNames();
        final CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
        final boolean hasUninitializedLazyProperties = persister.hasUninitializedLazyProperties(parent);
        final int componentPathStackDepth = 0;
        for (int i = 0; i < types.length; i++) {
            final CascadeStyle style = cascadeStyles[i];
            final String propertyName = propertyNames[i];
            final boolean isUninitializedProperty = hasUninitializedLazyProperties && !persister.getInstrumentationMetadata().isAttributeLoaded(parent, propertyName);
            if (style.doCascade(action)) {
                final Object child;
                if (isUninitializedProperty) {
                    // cascading to an uninitialized, lazy value.
                    if (types[i].isCollectionType()) {
                        // The collection does not need to be loaded from the DB.
                        // CollectionType#resolve will return an uninitialized PersistentCollection.
                        // The action will initialize the collection later, if necessary.
                        child = types[i].resolve(LazyPropertyInitializer.UNFETCHED_PROPERTY, eventSource, parent);
                    // TODO: it would be nice to be able to set the attribute in parent using
                    // persister.setPropertyValue( parent, i, child ).
                    // Unfortunately, that would cause the uninitialized collection to be
                    // loaded from the DB.
                    } else if (action.performOnLazyProperty()) {
                        // The (non-collection) attribute needs to be initialized so that
                        // the action can be performed on the initialized attribute.
                        LazyAttributeLoadingInterceptor interceptor = persister.getInstrumentationMetadata().extractInterceptor(parent);
                        child = interceptor.fetchAttribute(parent, propertyName);
                    } else {
                        // Nothing to do, so just skip cascading to this lazy (non-collection) attribute.
                        continue;
                    }
                } else {
                    child = persister.getPropertyValue(parent, i);
                }
                cascadeProperty(action, cascadePoint, eventSource, componentPathStackDepth, parent, child, types[i], style, propertyName, anything, false);
            } else {
                if (action.requiresNoCascadeChecking()) {
                    action.noCascade(eventSource, parent, persister, types[i], i);
                }
                // If the property is uninitialized, then there cannot be any orphans.
                if (action.deleteOrphans() && !isUninitializedProperty) {
                    cascadeLogicalOneToOneOrphanRemoval(action, eventSource, componentPathStackDepth, parent, persister.getPropertyValue(parent, i), types[i], style, propertyName, false);
                }
            }
        }
        if (traceEnabled) {
            LOG.tracev("Done processing cascade {0} for: {1}", action, persister.getEntityName());
        }
    }
}
Also used : CascadeStyle(org.hibernate.engine.spi.CascadeStyle) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) LazyAttributeLoadingInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor)

Aggregations

CascadeStyle (org.hibernate.engine.spi.CascadeStyle)3 AssociationType (org.hibernate.type.AssociationType)2 CollectionType (org.hibernate.type.CollectionType)2 CompositeType (org.hibernate.type.CompositeType)2 EntityType (org.hibernate.type.EntityType)2 Type (org.hibernate.type.Type)2 StringTokenizer (java.util.StringTokenizer)1 LazyAttributeLoadingInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor)1