Search in sources :

Example 6 with BackedSCO

use of org.datanucleus.store.types.wrappers.backed.BackedSCO in project datanucleus-rdbms by datanucleus.

the class CollectionMapping method postInsert.

/**
 * Method to be called after the insert of the owner class element.
 * @param ownerOP ObjectProvider of the owner
 */
public void postInsert(ObjectProvider ownerOP) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    Collection value = (Collection) ownerOP.provideField(getAbsoluteFieldNumber());
    if (containerIsStoredInSingleColumn()) {
        if (value != null) {
            if (mmd.getCollection().elementIsPersistent()) {
                // Make sure all persistable elements have ObjectProviders
                Object[] collElements = value.toArray();
                for (Object elem : collElements) {
                    if (elem != null) {
                        ObjectProvider elemOP = ec.findObjectProvider(elem);
                        if (elemOP == null || ec.getApiAdapter().getExecutionContext(elem) == null) {
                            elemOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, elem, false, ownerOP, mmd.getAbsoluteFieldNumber());
                        }
                    }
                }
            }
        }
        return;
    }
    if (value == null) {
        // replace null collections with an empty SCO wrapper
        replaceFieldWithWrapper(ownerOP, null);
        return;
    }
    Object[] collElements = value.toArray();
    if (!mmd.isCascadePersist()) {
        // Check that all elements are persistent before continuing and throw exception if necessary
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("007006", mmd.getFullFieldName()));
        }
        for (Object collElement : collElements) {
            if (!ec.getApiAdapter().isDetached(collElement) && !ec.getApiAdapter().isPersistent(collElement)) {
                // Element is not persistent so throw exception
                throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), collElement);
            }
        }
    } else {
        // Reachability
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("007007", mmd.getFullFieldName()));
        }
    }
    // Check if some elements need attaching
    // TODO Investigate if we can just use the attachCopy route below and skip off this check
    boolean needsAttaching = false;
    for (Object collElement : collElements) {
        if (ownerOP.getExecutionContext().getApiAdapter().isDetached(collElement)) {
            needsAttaching = true;
            break;
        }
    }
    if (needsAttaching) {
        // Create a wrapper and attach the elements (and add the others)
        SCO collWrapper = replaceFieldWithWrapper(ownerOP, null);
        if (value.size() > 0) {
            collWrapper.attachCopy(value);
            // The attach will have put entries in the operationQueue if using optimistic, so flush them
            ownerOP.getExecutionContext().flushOperationsForBackingStore(((BackedSCO) collWrapper).getBackingStore(), ownerOP);
        }
    } else {
        if (value.size() > 0) {
            // Add the elements direct to the datastore
            ((CollectionStore) storeMgr.getBackingStoreForField(ownerOP.getExecutionContext().getClassLoaderResolver(), mmd, value.getClass())).addAll(ownerOP, value, 0);
            // Create a SCO wrapper with the elements loaded
            replaceFieldWithWrapper(ownerOP, value);
        } else {
            if (mmd.getRelationType(ownerOP.getExecutionContext().getClassLoaderResolver()) == RelationType.MANY_TO_MANY_BI) {
                // Create a SCO wrapper, pass in null so it loads any from the datastore (on other side?)
                replaceFieldWithWrapper(ownerOP, null);
            } else {
                // Create a SCO wrapper, pass in empty collection to avoid loading from DB (extra SQL)
                replaceFieldWithWrapper(ownerOP, value);
            }
        }
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) Collection(java.util.Collection) ObjectProvider(org.datanucleus.state.ObjectProvider) CollectionStore(org.datanucleus.store.types.scostore.CollectionStore) SCO(org.datanucleus.store.types.SCO) BackedSCO(org.datanucleus.store.types.wrappers.backed.BackedSCO) ReachableObjectNotCascadedException(org.datanucleus.exceptions.ReachableObjectNotCascadedException)

Example 7 with BackedSCO

use of org.datanucleus.store.types.wrappers.backed.BackedSCO in project datanucleus-rdbms by datanucleus.

the class ArrayMapping method postInsert.

/**
 * Method to be called after the insert of the owner class element.
 * @param ownerOP ObjectProvider of the owner
 */
public void postInsert(ObjectProvider ownerOP) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    Object value = ownerOP.provideField(getAbsoluteFieldNumber());
    if (value == null) {
        return;
    }
    if (containerIsStoredInSingleColumn()) {
        if (mmd.getArray().elementIsPersistent()) {
            // Make sure all persistable elements have ObjectProviders
            Object[] arrElements = (Object[]) value;
            for (Object elem : arrElements) {
                if (elem != null) {
                    ObjectProvider elemOP = ec.findObjectProvider(elem);
                    if (elemOP == null || ec.getApiAdapter().getExecutionContext(elem) == null) {
                        elemOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, elem, false, ownerOP, mmd.getAbsoluteFieldNumber());
                    }
                }
            }
        }
        return;
    }
    int arrayLength = Array.getLength(value);
    boolean persistentElements = (mmd.getRelationType(ec.getClassLoaderResolver()) != RelationType.NONE);
    boolean needsAttaching = false;
    if (persistentElements) {
        Object[] array = (Object[]) value;
        if (!mmd.isCascadePersist()) {
            // Check that all elements are persistent before continuing and throw exception if necessary
            if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                NucleusLogger.PERSISTENCE.debug(Localiser.msg("007006", mmd.getFullFieldName()));
            }
            for (int i = 0; i < arrayLength; i++) {
                if (!ec.getApiAdapter().isDetached(array[i]) && !ec.getApiAdapter().isPersistent(array[i])) {
                    // Element is not persistent so throw exception
                    throw new ReachableObjectNotCascadedException(mmd.getFullFieldName(), array[i]);
                }
            }
        } else {
            // Reachability
            if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                NucleusLogger.PERSISTENCE.debug(Localiser.msg("007007", mmd.getFullFieldName()));
            }
        }
        for (int i = 0; i < arrayLength; i++) {
            if (ownerOP.getExecutionContext().getApiAdapter().isDetached(array[i])) {
                needsAttaching = true;
                break;
            }
        }
    }
    if (needsAttaching) {
        // Create a wrapper and attach the elements (and add the others)
        SCO collWrapper = replaceFieldWithWrapper(ownerOP, null);
        if (arrayLength > 0) {
            collWrapper.attachCopy(value);
            // The attach will have put entries in the operationQueue if using optimistic, so flush them
            ownerOP.getExecutionContext().flushOperationsForBackingStore(((BackedSCO) collWrapper).getBackingStore(), ownerOP);
        }
    } else {
        if (arrayLength > 0) {
            // Add the elements direct to the datastore
            ((ArrayStore) storeMgr.getBackingStoreForField(ownerOP.getExecutionContext().getClassLoaderResolver(), mmd, null)).set(ownerOP, value);
        }
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ObjectProvider(org.datanucleus.state.ObjectProvider) ArrayStore(org.datanucleus.store.types.scostore.ArrayStore) SCO(org.datanucleus.store.types.SCO) BackedSCO(org.datanucleus.store.types.wrappers.backed.BackedSCO) ReachableObjectNotCascadedException(org.datanucleus.exceptions.ReachableObjectNotCascadedException)

Example 8 with BackedSCO

use of org.datanucleus.store.types.wrappers.backed.BackedSCO in project datanucleus-rdbms by datanucleus.

the class MapMapping method preDelete.

/**
 * Method to be called before any delete of the owner class element.
 * @param ownerOP ObjectProvider of the owner
 */
public void preDelete(ObjectProvider ownerOP) {
    // Do nothing - dependent deletion is performed by deleteDependent()
    if (containerIsStoredInSingleColumn()) {
        // Do nothing when serialised since we are handled in the main request
        return;
    }
    // makes sure field is loaded
    ownerOP.isLoaded(getAbsoluteFieldNumber());
    java.util.Map value = (java.util.Map) ownerOP.provideField(getAbsoluteFieldNumber());
    if (value == null) {
        // Do nothing
        return;
    }
    if (!(value instanceof SCO)) {
        // Make sure we have a SCO wrapper so we can clear from the datastore
        value = (java.util.Map) SCOUtils.wrapSCOField(ownerOP, mmd.getAbsoluteFieldNumber(), value, true);
    }
    value.clear();
    // Flush any outstanding updates for this backing store
    ownerOP.getExecutionContext().flushOperationsForBackingStore(((BackedSCO) value).getBackingStore(), ownerOP);
}
Also used : Map(java.util.Map) Map(java.util.Map) SCO(org.datanucleus.store.types.SCO) BackedSCO(org.datanucleus.store.types.wrappers.backed.BackedSCO)

Aggregations

BackedSCO (org.datanucleus.store.types.wrappers.backed.BackedSCO)8 ExecutionContext (org.datanucleus.ExecutionContext)6 Collection (java.util.Collection)4 SCO (org.datanucleus.store.types.SCO)4 ObjectProvider (org.datanucleus.state.ObjectProvider)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)2 ReachableObjectNotCascadedException (org.datanucleus.exceptions.ReachableObjectNotCascadedException)2 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)2 SQLController (org.datanucleus.store.rdbms.SQLController)2 CollectionStore (org.datanucleus.store.types.scostore.CollectionStore)2 Iterator (java.util.Iterator)1 Set (java.util.Set)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 CollectionMetaData (org.datanucleus.metadata.CollectionMetaData)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1