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

UpdateAllQuery

public class UpdateAllQuery extends ModifyAllQuery
PUBLIC: A Query Class used to perform a bulk update using TopLink's expression framework. This class is provided to help optimize performance. It can be used in place of reading in all the objects to be changed and issuing single updates per instance. With this approach a single SQL UPDATE statement can be issued and then, based on the Expression provided, any objects in the cache that are effected by the update can be invalidated.

Notes:

  • By default, if a UOW is being used, this query will be deferred until the UOW commits.
  • UpdateAllQuery does not support foreign key updates unless the relationship is 1-1 (without back pointers.)

Example of Usage: Adding an area code.
UpdateAllQuery updateQuery = new UpdateAllQuery(Employee.class);
updateQuery.setSelectionCriteria(eb.get("areaCode").isNull());
updateQuery.addUpdate(eb.get("areaCode"), "613");

author
Guy Pelletier
date
March 1, 2004

Fields Summary
protected HashMap
m_updateClauses
Constructors Summary
public UpdateAllQuery()
PUBLIC: Constructs a default update all query.

        super();
    
public UpdateAllQuery(Class referenceClass)
PUBLIC: Constructs an update all query for the Class type specified.

param
referenceClass Class

        super(referenceClass);
    
public UpdateAllQuery(Class referenceClass, Expression selectionCriteria)
PUBLIC: Constructs an update all query for the specified Class type and selection criteria.

param
referenceClass Class type to be considered
param
selectionCriteria Expression

        super(referenceClass, selectionCriteria);
    
public UpdateAllQuery(Class referenceClass, ExpressionBuilder expressionBuilder)
PUBLIC: Constructs an update all query for the Class type specified and the given ExpressionBuilder. This sets the default builder which is used for all associated expressions in the query.

param
referenceClass Class type to be considered
param
builder ExpressionBuilder

        super(referenceClass);
        this.defaultBuilder = expressionBuilder;
    
Methods Summary
public voidaddUpdate(oracle.toplink.essentials.expressions.Expression field, java.lang.Object value)
PUBLIC: Adds the update (SET) clause to the query. Uses default ExpressionBuilder.

param
field Expression Object level representation of a database query 'where' clause
param
value Object, the new value

        addUpdateInternal(field, value);
    
public voidaddUpdate(java.lang.String attributeName, java.lang.Object value)
PUBLIC: Adds the update (SET) clause to the query. Uses default ExpressionBuilder.

param
attributeName String, the name of the attribute
param
value Object, the new value

        addUpdateInternal(attributeName, value);
    
public voidaddUpdate(oracle.toplink.essentials.expressions.Expression field, oracle.toplink.essentials.expressions.Expression value)
PUBLIC: Adds the update (SET) clause to the query. This method ensures that the builder has the session and reference class set for both given Expressions. Uses default ExpressionBuilder.

param
field Expression, representation of a database query 'where' clause that describes the field
param
value Expression, representation of a database query 'where' clause that describes the new value

        addUpdateInternal(field, value);
    
public voidaddUpdate(java.lang.String attributeName, oracle.toplink.essentials.expressions.Expression value)
PUBLIC: Adds the update (SET) clause to the query. Uses default ExpressionBuilder.

param
attributeName String, the name of the attribute
param
value Expression, the new value

        addUpdateInternal(attributeName, value);
    
protected voidaddUpdateInternal(java.lang.Object fieldObject, java.lang.Object valueObject)
INTERNAL:

        if(fieldObject == null) {
            throw QueryException.updateAllQueryAddUpdateFieldIsNull(this);
        }
        if(m_updateClauses == null) {
            m_updateClauses = new HashMap();
        }
        m_updateClauses.put(fieldObject, valueObject);
    
public java.lang.ObjectexecuteDatabaseQuery()
INTERNAL: Issue the SQL to the database and then merge into the cache. If we are within a UoW, the merge to the cache must not be done until the UoW merges into the parent. The UoW will trigger the merge to occur at the correct time and will ensure the cache setting is set to none at that time.

        result = getQueryMechanism().updateAll();// fire the SQL to the database
        mergeChangesIntoSharedCache();
        return result;
    
public java.util.HashMapgetUpdateClauses()
INTERNAL: Return the updates stored for an update all query

        return m_updateClauses;
    
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.

        super.initializeQuerySpecificDefaultBuilder();
        if(this.defaultBuilder == null && m_updateClauses != null) {
            Iterator it = m_updateClauses.values().iterator();
            while(it.hasNext() ) {
                Object value = it.next();
                if(value != null) {
                    if(value instanceof Expression) {
                        Expression valueExpression = (Expression)value;
                        this.defaultBuilder = valueExpression.getBuilder();
                        if(this.defaultBuilder != null) {
                            return;
                        }
                    }
                }
            }
            it = m_updateClauses.keySet().iterator();
            while(it.hasNext() ) {
                Object field = it.next();
                if(field instanceof Expression) {
                    Expression fieldExpression = (Expression)field;
                    this.defaultBuilder = fieldExpression.getBuilder();
                    if(this.defaultBuilder != null) {
                        return;
                    }
                }
            }
        }
    
public booleanisUpdateAllQuery()
INTERNAL: Return true if this is an update all query.

        return true;
    
protected voidprepare()
INTERNAL:

        super.prepare();// will tell the query mechanism to prepare itself as well.

        Class referenceClass = getReferenceClass();

        // Check the reference class, must be set
        if (referenceClass == null) {
            throw QueryException.referenceClassMissing(this);
        }

        // Check the descriptor is set, if not try to get it from the session
        if (getDescriptor() == null) {
            ClassDescriptor desc = getSession().getDescriptor(referenceClass);

            if (desc == null) {
                throw QueryException.descriptorIsMissing(referenceClass, this);
            }

            setDescriptor(desc);
        }

        ClassDescriptor descriptor = getDescriptor();

        // Check the descriptor for an aggregate
        if (descriptor.isAggregateDescriptor()) {
            throw QueryException.aggregateObjectCannotBeDeletedOrWritten(descriptor, this);
        }

        // Check that the update statement is set. That is the bare minimum,
        // the user can execute this query without a selection criteria though.
        if ((getUpdateClauses() == null || getUpdateClauses().isEmpty()) && isExpressionQuery()) {
            throw QueryException.updateStatementsNotSpecified();
        }
        
        getQueryMechanism().prepareUpdateAll();