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

WriteObjectQuery

public class WriteObjectQuery extends ObjectLevelModifyQuery

Purpose: Used for inserting or updating objects WriteObjectQuery determines whether to perform a insert or an update on the database.

Responsibilities:

  • Determines whether to perform a insert or an update on the database.
  • Stores object in identity map for insert if required.
author
Yvon Lavoie
since
TOPLink/Java 1.0

Fields Summary
Constructors Summary
public WriteObjectQuery()

        super();
    
public WriteObjectQuery(Object objectToWrite)

        this();
        setObject(objectToWrite);
    
public WriteObjectQuery(Call call)

        this();
        setCall(call);
    
Methods Summary
protected booleandoesObjectExist()
INTERNAL: Return if the object exists on the database or not. This first checks existence in the chache, then on the database.

        boolean doesExist;

        if (getSession().isUnitOfWork()) {
            doesExist = !((UnitOfWorkImpl)getSession()).isCloneNewObject(getObject());
            if (doesExist) {
                doesExist = ((UnitOfWorkImpl)getSession()).isObjectRegistered(getObject());
            }
        } else {
            //Initialize does exist query
            DoesExistQuery existQuery = (DoesExistQuery)getDescriptor().getQueryManager().getDoesExistQuery().clone();
            existQuery.setObject(getObject());
            existQuery.setPrimaryKey(getPrimaryKey());
            existQuery.setDescriptor(getDescriptor());
            existQuery.setTranslationRow(getTranslationRow());

            doesExist = ((Boolean)getSession().executeQuery(existQuery)).booleanValue();
        }
        
        return doesExist;
    
public voidexecuteCommit()
INTERNAL: Decide whether to perform an insert, update or delete and delegate the work to the mechanism.

        boolean doesExist = doesObjectExist();
        boolean shouldBeDeleted = shouldObjectBeDeleted();

        // Do insert, update or delete                    
        if (doesExist) {
            if (shouldBeDeleted) {
                // Must do a delete
                getQueryMechanism().deleteObjectForWrite();
            } else {
                // Must do an update            
                getQueryMechanism().updateObjectForWrite();
            }
        } else if (!shouldBeDeleted) {
            // Must do an insert
            getQueryMechanism().insertObjectForWrite();
        }
    
public voidexecuteCommitWithChangeSet()
INTERNAL: Perform a does exist check to decide whether to perform an insert or update and delegate the work to the mechanism.

        // Do insert of update                    
        if (!getObjectChangeSet().isNew()) {
            // Must do an update            
            if (!getSession().getCommitManager().isCommitInPreModify(objectChangeSet)) {
                //If the changeSet is in the PreModify then it is in the process of being written
                getQueryMechanism().updateObjectForWriteWithChangeSet();
            }
        } else {
            // check whether the object is already being committed -
            // if it is and it is new, then a shallow insert must be done
            if (getSession().getCommitManager().isCommitInPreModify(objectChangeSet)) {
                // a shallow insert must be performed
                this.dontCascadeParts();
                getQueryMechanism().insertObjectForWriteWithChangeSet();
                getSession().getCommitManager().markShallowCommit(object);
            } else {
                // Must do an insert
                getQueryMechanism().insertObjectForWriteWithChangeSet();
            }
        }
    
public java.lang.ObjectexecuteDatabaseQuery()
INTERNAL: Perform a does exist check to decide whether to perform an insert or update and delegate the work to the mechanism. Does exists check will also perform an optimistic lock check if required.

exception
DatabaseException - an error has occurred on the database
exception
OptimisticLockException - an error has occurred using the optimistic lock feature
return
object - the object being written.

        if (getObjectChangeSet() != null) {
            return getQueryMechanism().executeWriteWithChangeSet();
        } else {
            return getQueryMechanism().executeWrite();
        }
    
public voidexecuteShallowWrite()
INTERNAL: Perform a shallow write. The decision, which shallow action should be executed is based on the existence of the associated object. If the object exists, perform a shallow delete. Do a shallow insert otherwise. Note that there currently is *no* shallow update operation. If shallow updates become necessary, the decision logic must also perform a delete check as in {@link this.executeCommit}.

        boolean doesExist = doesObjectExist();

        // Shallow writes only occur for inserts or deletes
        if (doesExist) {
            getQueryMechanism().shallowDeleteObjectForWrite(getObject(), this, getSession().getCommitManager());
        } else {
            getQueryMechanism().shallowInsertObjectForWrite(getObject(), this, getSession().getCommitManager());
        }
    
public booleanisWriteObjectQuery()
PUBLIC: Return if this is a write object query.

        return true;
    
public voidprepareForExecution()
INTERNAL: Prepare the receiver for execution in a session.

        super.prepareForExecution();

        // Set the tranlation row, it may already be set in the custom query situation.
        if ((getTranslationRow() == null) || (getTranslationRow().isEmpty())) {
            setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getObject(), getSession()));
        }
    
public booleanshouldDependentObjectBeDeleted(java.lang.Object object)
INTERNAL: Return whether a dependent object should be deleted from the database or not. Dependent objects should not be removed if not already scheduled for removal in a UoW. Returns "true" outside a UoW. Used by relationship mappings when cascading a delete operation.

        boolean shouldBeDeleted;
        
        if (getSession().isUnitOfWork()) {
            shouldBeDeleted = ((UnitOfWorkImpl)getSession()).isObjectDeleted(object);
        }  else {
            // Deletes are cascaded outside a UoW
            shouldBeDeleted = true;
        }
        
        return shouldBeDeleted;
    
protected booleanshouldObjectBeDeleted()
INTERNAL: Return if the attached object should be deleted from the database or not. This information is available only, if the session is a UoW. Returns "false" outside a UoW. In this case an existence check should be performed and either an insert or update executed. Only used internally.

        boolean shouldBeDeleted;
        
        if (getSession().isUnitOfWork()) {
            shouldBeDeleted = ((UnitOfWorkImpl)getSession()).isObjectDeleted(getObject());
        }  else {
            // Deletes must be explicitly user defined outside a UoW
            shouldBeDeleted = false;
        }
        
        return shouldBeDeleted;