/** INTERNAL: Update privately owned parts */
  public void preUpdate(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!isAttributeValueInstantiated(query.getObject())) {
      return;
    }

    if (isPrivateOwned()) {
      Object objectInDatabase = readPrivateOwnedForObject(query);
      if (objectInDatabase != null) {
        query.setProperty(this, objectInDatabase);
      }
    }

    if (!shouldObjectModifyCascadeToParts(query)) {
      return;
    }

    // Get the privately owned parts in the memory
    Object object = getRealAttributeValueFromObject(query.getObject(), query.getSession());
    if (object != null) {
      ObjectChangeSet changeSet = null;
      UnitOfWorkChangeSet uowChangeSet = null;
      if (query.getSession().isUnitOfWork()
          && (((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet() != null)) {
        uowChangeSet =
            (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet();
        changeSet = (ObjectChangeSet) uowChangeSet.getObjectChangeSetForClone(object);
      }
      WriteObjectQuery writeQuery = new WriteObjectQuery();
      writeQuery.setIsExecutionClone(true);
      writeQuery.setObject(object);
      writeQuery.setObjectChangeSet(changeSet);
      writeQuery.setCascadePolicy(query.getCascadePolicy());
      query.getSession().executeQuery(writeQuery);
    }
  }
  /** INTERNAL: Insert privately owned parts */
  public void preInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!this.isForeignKeyRelationship()) {
      return;
    }

    if (!this.shouldObjectModifyCascadeToParts(query)) {
      return;
    }

    // only cascade dependents in UOW
    if (query.shouldCascadeOnlyDependentParts()) {
      return;
    }

    Object objects =
        this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());

    // insert each object one by one
    ContainerPolicy cp = this.getContainerPolicy();
    for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
      Object object = cp.next(iter, query.getSession());
      if (this.isPrivateOwned()) {
        // no need to set changeset here as insert is just a copy of the object anyway
        InsertObjectQuery insertQuery = new InsertObjectQuery();
        insertQuery.setIsExecutionClone(true);
        insertQuery.setObject(object);
        insertQuery.setCascadePolicy(query.getCascadePolicy());
        query.getSession().executeQuery(insertQuery);
      } else {
        // This will happen in a unit of work or cascaded query.
        // This is done only for persistence by reachability and is not required if the targets are
        // in the queue anyway
        // Avoid cycles by checking commit manager, this is allowed because there is no dependency.
        if (!query.getSession().getCommitManager().isCommitInPreModify(object)) {
          WriteObjectQuery writeQuery = new WriteObjectQuery();
          writeQuery.setIsExecutionClone(true);
          if (query.getSession().isUnitOfWork()) {
            UnitOfWorkChangeSet uowChangeSet =
                (UnitOfWorkChangeSet)
                    ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet();
            if (uowChangeSet != null) {
              writeQuery.setObjectChangeSet(
                  (ObjectChangeSet) uowChangeSet.getObjectChangeSetForClone(object));
            }
          }
          writeQuery.setObject(object);
          writeQuery.setCascadePolicy(query.getCascadePolicy());
          query.getSession().executeQuery(writeQuery);
        }
      }
    }
  }
 /**
  * Commit changed of the objects of the class type in the change set. This allows for the order of
  * the classes to be processed optimally.
  */
 protected void commitChangedObjectsForClassWithChangeSet(
     UnitOfWorkChangeSet uowChangeSet, Class theClass) {
   Map<ObjectChangeSet, ObjectChangeSet> objectChangesList =
       uowChangeSet.getObjectChanges().get(theClass);
   if (objectChangesList != null) { // may be no changes for that class type.		
     ClassDescriptor descriptor = null;
     AbstractSession session = getSession();
     Collection<ObjectChangeSet> changes = objectChangesList.values();
     if (((UnitOfWorkImpl) session).shouldOrderUpdates()) {
       changes = new ArrayList(objectChangesList.values());
       Collections.sort((List) changes);
     }
     for (ObjectChangeSet changeSetToWrite : changes) {
       Object objectToWrite = changeSetToWrite.getUnitOfWorkClone();
       if (descriptor == null) {
         descriptor = session.getDescriptor(objectToWrite);
       }
       if (!isProcessedCommit(objectToWrite)) {
         // Commit and resume on failure can cause a new change set to be in existing, so need to
         // check here.
         WriteObjectQuery commitQuery = null;
         if (changeSetToWrite.isNew()) {
           commitQuery = new InsertObjectQuery();
         } else {
           commitQuery = new UpdateObjectQuery();
         }
         commitQuery.setIsExecutionClone(true);
         commitQuery.setDescriptor(descriptor);
         commitQuery.setObjectChangeSet(changeSetToWrite);
         commitQuery.setObject(objectToWrite);
         commitQuery.cascadeOnlyDependentParts();
         // removed checking session type to set cascade level
         // will always be a unitOfWork so we need to cascade dependent parts
         session.executeQuery(commitQuery);
       }
     }
   }
 }
  /** INTERNAL: Insert privately owned parts */
  public void preInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException {
    // Checks if privately owned parts should be inserted or not.
    if (!shouldObjectModifyCascadeToParts(query)) {
      return;
    }

    // Get the privately owned parts
    Object object = getRealAttributeValueFromObject(query.getObject(), query.getSession());

    if (object == null) {
      return;
    }

    if (isPrivateOwned()) {
      // No need to set changeSet as insert is a straight copy anyway
      InsertObjectQuery insertQuery = new InsertObjectQuery();
      insertQuery.setIsExecutionClone(true);
      insertQuery.setObject(object);
      insertQuery.setCascadePolicy(query.getCascadePolicy());
      query.getSession().executeQuery(insertQuery);
    } else {
      ObjectChangeSet changeSet = null;
      UnitOfWorkChangeSet uowChangeSet = null;
      if (query.getSession().isUnitOfWork()
          && (((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet() != null)) {
        uowChangeSet =
            (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet();
        changeSet = (ObjectChangeSet) uowChangeSet.getObjectChangeSetForClone(object);
      }
      WriteObjectQuery writeQuery = new WriteObjectQuery();
      writeQuery.setIsExecutionClone(true);
      writeQuery.setObject(object);
      writeQuery.setObjectChangeSet(changeSet);
      writeQuery.setCascadePolicy(query.getCascadePolicy());
      query.getSession().executeQuery(writeQuery);
    }
  }