FileDocCategorySizeDatePackage
ModifyAllQuery.javaAPI DocGlassfish v2 API13017Tue May 22 16:54:50 BST 2007oracle.toplink.essentials.queryframework

ModifyAllQuery

public abstract class ModifyAllQuery extends ModifyQuery
PUBLIC: Query used to perform a bulk delete using TopLink's expression framework.
author
Andrei Ilitchev
date
August 18, 2005

Fields Summary
public static final int
NO_CACHE
Cache usage flags
public static final int
INVALIDATE_CACHE
private int
m_cacheUsage
protected Class
referenceClass
protected String
referenceClassName
protected transient Integer
result
Number of modified objects
private boolean
shouldDeferExecutionInUOW
Indicates whether execution should be deferred in UOW
protected ExpressionBuilder
defaultBuilder
Provide a default builder so that it's easier to be consistent
protected boolean
isPreparedUsingTempStorage
Indicates whether the query was prepared so that it will execute using temp storage
Constructors Summary
public ModifyAllQuery()
PUBLIC:

    
          
      
        super();
        shouldDeferExecutionInUOW = true;
    
public ModifyAllQuery(Class referenceClass)
PUBLIC: Create a new update all query for the class specified.

        this();
        setReferenceClass(referenceClass);
    
public ModifyAllQuery(Class referenceClass, Expression selectionCriteria)
PUBLIC: Create a new update all query for the class and the selection criteria specified.

        this();
        setReferenceClass(referenceClass);
        setSelectionCriteria(selectionCriteria);
    
Methods Summary
protected voidclonedQueryExecutionComplete(oracle.toplink.essentials.queryframework.DatabaseQuery query, oracle.toplink.essentials.internal.sessions.AbstractSession session)
INTERNAL Used to give the subclasses oportunity to copy aspects of the cloned query to the original query. The clones of all the ModifyAllQueries will be added to modifyAllQueries for validation.

        super.clonedQueryExecutionComplete(query, session);
        
        if (session.isUnitOfWork()) {
            ((UnitOfWorkImpl)session).storeModifyAllQuery(query);
        }
    
public java.lang.ObjectexecuteInUnitOfWork(oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl unitOfWork, oracle.toplink.essentials.internal.sessions.AbstractRecord translationRow)
INTERNAL: Override query execution where Session is a UnitOfWork.

If there are objects in the cache return the results of the cache lookup.

param
unitOfWork - the session in which the receiver will be executed.
param
translationRow - the arguments
exception
DatabaseException - an error has occurred on the database.
exception
OptimisticLockException - an error has occurred using the optimistic lock feature.
return
An object, the result of executing the query.

        if (unitOfWork.isNestedUnitOfWork()) {
            throw ValidationException.nestedUOWNotSupportedForModifyAllQuery();
        }

        //Bug4607551  For UpdateAllQuery, if deferred, add the original query with a translation row to the deferredUpdateAllQueries for execution.  
        //No action for non-deferred.  Later on the clones of all the UpdateAllQuery's will be added to modifyAllQueries for validation.
        if(shouldDeferExecutionInUOW()) {
            unitOfWork.storeDeferredModifyAllQuery(this, translationRow);
            result = null;
        } else {
            if(!unitOfWork.isInTransaction()) {
                unitOfWork.beginEarlyTransaction();
            }
            unitOfWork.setWasNonObjectLevelModifyQueryExecuted(true);
            result = (Integer)super.executeInUnitOfWork(unitOfWork, translationRow);
        }
        return result;
    
public intgetCacheUsage()
PUBLIC: Return the cache usage for this query.

        return m_cacheUsage;
    
public oracle.toplink.essentials.expressions.ExpressionBuildergetExpressionBuilder()
PUBLIC: Get the expression builder which should be used for this query. This expression builder should be used to build all expressions used by this query.

        if (defaultBuilder == null) {
            initializeDefaultBuilder();
        }

        return defaultBuilder;
    
public java.lang.ClassgetReferenceClass()
PUBLIC: Return the reference class for this query.

        return referenceClass;
    
public java.lang.StringgetReferenceClassName()
INTERNAL: Return the name of the reference class of the query. Used by the Mappign Workbench to avoid classpath dependancies

        if ((referenceClassName == null) && (referenceClass != null)) {
            referenceClassName = referenceClass.getName();
        }
        return referenceClassName;
    
protected voidinitializeDefaultBuilder()
INTERNAL: Initialize the expression builder which should be used for this query. If there is a where clause, use its expression builder, otherwise generate one and cache it. This helps avoid unnecessary rebuilds.

        initializeQuerySpecificDefaultBuilder();
        if(defaultBuilder == null) {
            defaultBuilder = new ExpressionBuilder();
        }
    
protected voidinitializeQuerySpecificDefaultBuilder()
INTERNAL: Initialize the expression builder which should be used for this query. If there is a where clause, use its expression builder. If after this method defaultBuilder is still null, then initializeDefaultBuilder method will generate and cache it.

        DatabaseQueryMechanism mech = getQueryMechanism();
        if (mech.isExpressionQueryMechanism() && ((ExpressionQueryMechanism)mech).getExpressionBuilder() != null) {
            this.defaultBuilder = ((ExpressionQueryMechanism)mech).getExpressionBuilder();
        }
    
protected voidinvalidateCache()
INTERNAL: Invalid the cache, that is, those objects in the cache that were affected by the query.

        oracle.toplink.essentials.sessions.IdentityMapAccessor identityMapAccessor = getSession().getIdentityMapAccessor();
        if (getSelectionCriteria() == null) {
            // Invalidate the whole class since the user did not specify a where clause
            if(getDescriptor().isChildDescriptor()) {
                Vector collectionToInvalidate = identityMapAccessor.getAllFromIdentityMap(null, getReferenceClass(), getTranslationRow(), null);
                identityMapAccessor.invalidateObjects(collectionToInvalidate);
            } else {
                // if it's either a root class or there is no inheritance just clear the identity map
                identityMapAccessor.invalidateClass(getReferenceClass());
            }
        } else {
            // Invalidate only those objects in the cache that match the selection criteria
            //Bug:4293920, expression parameters were not passed in
            boolean noObjectsModifiedInDb = result != null && result.intValue() == 0;
            try {
                InMemoryQueryIndirectionPolicy policy = new InMemoryQueryIndirectionPolicy();
                if(noObjectsModifiedInDb) {
                    policy.ignoreIndirectionExceptionReturnNotConformed();
                } else {
                    policy.ignoreIndirectionExceptionReturnConformed();
                }
                Vector collectionToInvalidate = identityMapAccessor.getAllFromIdentityMap(getSelectionCriteria(), getReferenceClass(), getTranslationRow(), policy);
                identityMapAccessor.invalidateObjects(collectionToInvalidate);
            } catch (QueryException ex) {
                if(ex.getErrorCode() == QueryException.CANNOT_CONFORM_EXPRESSION) {
                    // If no objects changed in the db - don't invalidate, ignore.
                    if(!noObjectsModifiedInDb) {
                        // Invalidate the whole class since the expression can't be selected in memory
                        identityMapAccessor.invalidateClass(getReferenceClass());
                    }
                } else {
                    throw ex;
                }
            }
        }
    
public booleanisModifyQuery()
PUBLIC: Return true if this is a modify query.

        return true;
    
public booleanisPreparedUsingTempStorage()
INTERNAL:

        return isPreparedUsingTempStorage;
    
public voidmergeChangesIntoSharedCache()
INTERNAL: After execution we need to merge the changes into the shared cache

        if (shouldInvalidateCache()) {
            invalidateCache();
        }
    
public voidsetCacheUsage(int cacheUsage)
PUBLIC: Set the level of cache support for this query, either NONE or INVALIDATE.

        m_cacheUsage = cacheUsage;
    
public voidsetExpressionBuilder(oracle.toplink.essentials.expressions.ExpressionBuilder builder)
INTERNAL Sets the default expression builder for this query.

        this.defaultBuilder = builder;
    
public voidsetIsPreparedUsingTempStorage(boolean isPreparedUsingTempStorage)
INTERNAL:

        this.isPreparedUsingTempStorage = isPreparedUsingTempStorage;
    
public voidsetReferenceClass(java.lang.Class referenceClass)
PUBLIC: Set the reference class this query.

        if (this.referenceClass != referenceClass) {
            setIsPrepared(false);
        }
        this.referenceClass = referenceClass;
    
public voidsetReferenceClassName(java.lang.String className)
INTERNAL: Set the class name of the reference class of this query. Used by the Mapping Workbench to avoid classpath dependancies.

        referenceClassName = className;
    
public voidsetShouldDeferExecutionInUOW(boolean shouldDeferExecutionInUOW)
PUBLIC: Set a flag indicating whether execution should be deferred in UOW until commit.

        this.shouldDeferExecutionInUOW = shouldDeferExecutionInUOW;
    
public booleanshouldDeferExecutionInUOW()
PUBLIC: Indicates whether execution should be deferred in UOW until commit.

        return shouldDeferExecutionInUOW;
    
protected booleanshouldInvalidateCache()
INTERNAL:

        return m_cacheUsage == INVALIDATE_CACHE;