protected Object instantiate(AbstractSession session) throws DatabaseException {
    Vector rows = mapping.getForeignKeyRows(this.getRow());

    int size = rows.size();
    ContainerPolicy cp = ((ReadAllQuery) this.getQuery()).getContainerPolicy();
    Object returnValue = cp.containerInstance(size);

    for (int i = 0; i < size; i++) {
      AbstractRecord nextRow = (AbstractRecord) rows.get(i);
      Object results = session.executeQuery(getQuery(), nextRow);

      if (results instanceof Collection) {
        Iterator iter = ((Collection) results).iterator();
        while (iter.hasNext()) {
          cp.addInto(iter.next(), returnValue, session);
        }
      } else if (results instanceof java.util.Map) {
        Iterator iter = ((java.util.Map) results).values().iterator();
        while (iter.hasNext()) {
          cp.addInto(iter.next(), returnValue, session);
        }
      } else {
        cp.addInto(results, returnValue, session);
      }
    }
    return returnValue;
  }
  /**
   * Merge changes from the source to the target object. Make the necessary removals and adds and
   * map key modifications.
   */
  private void mergeChangesIntoObjectWithoutOrder(
      Object target, ChangeRecord changeRecord, Object source, MergeManager mergeManager) {
    EISCollectionChangeRecord sdkChangeRecord = (EISCollectionChangeRecord) changeRecord;
    ContainerPolicy cp = this.getContainerPolicy();
    AbstractSession session = mergeManager.getSession();

    Object targetCollection = null;
    if (sdkChangeRecord.getOwner().isNew()) {
      targetCollection = cp.containerInstance(sdkChangeRecord.getAdds().size());
    } else {
      targetCollection = this.getRealCollectionAttributeValueFromObject(target, session);
    }

    Vector removes = sdkChangeRecord.getRemoves();
    Vector adds = sdkChangeRecord.getAdds();
    Vector changedMapKeys = sdkChangeRecord.getChangedMapKeys();

    synchronized (targetCollection) {
      for (Enumeration stream = removes.elements(); stream.hasMoreElements(); ) {
        Object removeElement =
            this.buildRemovedElementFromChangeSet(stream.nextElement(), mergeManager);

        Object targetElement = null;
        for (Object iter = cp.iteratorFor(targetCollection); cp.hasNext(iter); ) {
          targetElement = cp.next(iter, session);
          if (this.compareElements(targetElement, removeElement, session)) {
            break; // matching element found - skip the rest of them
          }
        }
        if (targetElement != null) {
          // a matching element was found, remove it
          cp.removeFrom(targetElement, targetCollection, session);
        }
      }

      for (Enumeration stream = adds.elements(); stream.hasMoreElements(); ) {
        Object addElement = this.buildAddedElementFromChangeSet(stream.nextElement(), mergeManager);
        cp.addInto(addElement, targetCollection, session);
      }

      for (Enumeration stream = changedMapKeys.elements(); stream.hasMoreElements(); ) {
        Object changedMapKeyElement =
            this.buildAddedElementFromChangeSet(stream.nextElement(), mergeManager);
        Object originalElement =
            ((UnitOfWorkImpl) session).getOriginalVersionOfObject(changedMapKeyElement);
        cp.removeFrom(originalElement, targetCollection, session);
        cp.addInto(changedMapKeyElement, targetCollection, session);
      }
    }

    // reset the attribute to allow for set method to re-morph changes if the collection is not
    // being stored directly
    this.setRealAttributeValueInObject(target, targetCollection);
  }
  /**
   * INTERNAL: Merge changes from the source to the target object. Simply replace the entire target
   * collection.
   */
  public void mergeIntoObject(
      Object target, boolean isTargetUnInitialized, Object source, MergeManager mergeManager) {
    ContainerPolicy cp = this.getContainerPolicy();
    AbstractSession session = mergeManager.getSession();

    Object sourceCollection = this.getRealCollectionAttributeValueFromObject(source, session);
    Object targetCollection = cp.containerInstance(cp.sizeFor(sourceCollection));

    for (Object iter = cp.iteratorFor(sourceCollection); cp.hasNext(iter); ) {
      Object targetElement = this.buildElementFromElement(cp.next(iter, session), mergeManager);
      cp.addInto(targetElement, targetCollection, session);
    }

    // reset the attribute to allow for set method to re-morph changes if the collection is not
    // being stored directly
    this.setRealAttributeValueInObject(target, targetCollection);
  }
  /**
   * Merge changes from the source to the target object. Simply replace the entire target
   * collection.
   */
  private void mergeChangesIntoObjectWithOrder(
      Object target, ChangeRecord changeRecord, Object source, MergeManager mergeManager) {
    ContainerPolicy cp = this.getContainerPolicy();
    AbstractSession session = mergeManager.getSession();

    Vector changes = ((EISOrderedCollectionChangeRecord) changeRecord).getNewCollection();
    Object targetCollection = cp.containerInstance(changes.size());

    for (Enumeration stream = changes.elements(); stream.hasMoreElements(); ) {
      Object targetElement =
          this.buildAddedElementFromChangeSet(stream.nextElement(), mergeManager);
      cp.addInto(targetElement, targetCollection, session);
    }

    // reset the attribute to allow for set method to re-morph changes if the collection is not
    // being stored directly
    this.setRealAttributeValueInObject(target, targetCollection);
  }
 /**
  * INTERNAL: Execute the query. If there are cached results return those. This must override the
  * super to support result caching.
  *
  * @param session - the session in which the receiver will be executed.
  * @return An object or vector, the result of executing the query.
  * @exception DatabaseException - an error has occurred on the database
  */
 public Object execute(AbstractSession session, AbstractRecord row) throws DatabaseException {
   if (shouldCacheQueryResults()) {
     if (getContainerPolicy().overridesRead()) {
       throw QueryException.cannotCacheCursorResultsOnQuery(this);
     }
     if (shouldConformResultsInUnitOfWork()) {
       throw QueryException.cannotConformAndCacheQueryResults(this);
     }
     if (isPrepared()) { // only prepared queries can have cached results.
       Object queryResults = getQueryResults(session, row, true);
       if (queryResults != null) {
         if (QueryMonitor.shouldMonitor()) {
           QueryMonitor.incrementReadAllHits(this);
         }
         // bug6138532 - check for "cached no results" (InvalidObject singleton) in query
         // results, and return an empty container instance as configured
         if (queryResults == InvalidObject.instance) {
           return getContainerPolicy().containerInstance(0);
         }
         Collection results = (Collection) queryResults;
         if (session.isUnitOfWork()) {
           ContainerPolicy policy = getContainerPolicy();
           Object resultCollection = policy.containerInstance(results.size());
           Object iterator = policy.iteratorFor(results);
           while (policy.hasNext(iterator)) {
             Object result =
                 ((UnitOfWorkImpl) session)
                     .registerExistingObject(policy.next(iterator, session), this.descriptor);
             policy.addInto(result, resultCollection, session);
           }
           return resultCollection;
         }
         return results;
       }
     }
   }
   if (QueryMonitor.shouldMonitor()) {
     QueryMonitor.incrementReadAllMisses(this);
   }
   return super.execute(session, row);
 }
 /**
  * INTERNAL: The results are *not* in a cursor, build the collection. Cache the results in
  * temporaryCachedQueryResults.
  */
 protected Object executeNonCursor() throws DatabaseException {
   Vector rows = getQueryMechanism().executeSelect();
   Object results = null;
   if (this.resultType == MAP) {
     results = getContainerPolicy().buildContainerFromVector(rows, this.session);
   } else if (this.resultType == VALUE) {
     if (!rows.isEmpty()) {
       AbstractRecord record = (AbstractRecord) rows.get(0);
       // Use get with field for XML records.
       results = record.get(record.getFields().get(0));
       if (getValueConverter() != null) {
         results = getValueConverter().convertDataValueToObjectValue(results, this.session);
       }
     }
   } else {
     int size = rows.size();
     ContainerPolicy containerPolicy = getContainerPolicy();
     results = containerPolicy.containerInstance(size);
     if (containerPolicy.shouldAddAll()) {
       if (size > 0) {
         List values = new ArrayList(size);
         for (int index = 0; index < size; index++) {
           AbstractRecord row = (AbstractRecord) rows.get(index);
           Object value = buildObject(row);
           values.add(value);
         }
         containerPolicy.addAll(values, results, this.session, rows, this);
       }
     } else {
       for (int index = 0; index < size; index++) {
         AbstractRecord row = (AbstractRecord) rows.get(index);
         Object value = buildObject(row);
         containerPolicy.addInto(value, results, this.session, row, this);
       }
     }
   }
   // Bug 6135563 - cache DataReadQuery results verbatim, as ObjectBuilder is not invoked
   cacheResult(results);
   return results;
 }
  /**
   * INTERNAL: Return the value of the reference attribute or a value holder. Check whether the
   * mapping's attribute should be optimized through batch and joining.
   */
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery sourceQuery,
      AbstractSession executionSession)
      throws DatabaseException {
    if (((EISDescriptor) this.getDescriptor()).getDataFormat() == EISDescriptor.XML) {
      ((XMLRecord) row).setSession(executionSession);
    }

    ReadQuery targetQuery = getSelectionQuery();
    if (!isForeignKeyRelationship) {
      // if the source query is cascading then the target query must use the same settings
      if (targetQuery.isObjectLevelReadQuery()
          && (sourceQuery.shouldCascadeAllParts()
              || (sourceQuery.shouldCascadePrivateParts() && isPrivateOwned())
              || (sourceQuery.shouldCascadeByMapping() && this.cascadeRefresh))) {
        targetQuery = (ObjectLevelReadQuery) targetQuery.clone();
        ((ObjectLevelReadQuery) targetQuery)
            .setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
        targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
        // CR #4365
        targetQuery.setQueryId(sourceQuery.getQueryId());
        // For queries that have turned caching off, such as aggregate collection, leave it off.
        if (targetQuery.shouldMaintainCache()) {
          targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
        }
      }

      return getIndirectionPolicy().valueFromQuery(targetQuery, row, sourceQuery.getSession());
    } else {
      if (getIndirectionPolicy().usesIndirection()) {
        EISOneToManyQueryBasedValueHolder valueholder =
            new EISOneToManyQueryBasedValueHolder(this, targetQuery, row, sourceQuery.getSession());
        return valueholder;
      } else {
        Vector subRows = getForeignKeyRows(row);

        if (subRows == null) {
          return null;
        }

        ContainerPolicy cp = this.getContainerPolicy();
        Object results = cp.containerInstance(subRows.size());

        for (int i = 0; i < subRows.size(); i++) {
          XMLRecord subRow = (XMLRecord) subRows.elementAt(i);
          subRow.setSession(executionSession);
          Object object =
              getIndirectionPolicy().valueFromQuery(targetQuery, subRow, sourceQuery.getSession());
          if (object instanceof Collection) {
            java.util.Iterator iter = ((Collection) object).iterator();
            while (iter.hasNext()) {
              cp.addInto(iter.next(), results, executionSession);
            }
          } else if (object instanceof java.util.Map) {
            java.util.Iterator iter = ((java.util.Map) object).values().iterator();
            while (iter.hasNext()) {
              cp.addInto(iter.next(), results, executionSession);
            }
          } else {
            cp.addInto(object, results, executionSession);
          }
        }
        if (cp.sizeFor(results) == 0) {
          return null;
        }
        return results;
      }
    }
  }
  /**
   * INTERNAL: All objects queried via a UnitOfWork get registered here. If the query went to the
   * database.
   *
   * <p>Involves registering the query result individually and in totality, and hence refreshing /
   * conforming is done here.
   *
   * @param result may be collection (read all) or an object (read one), or even a cursor. If in
   *     transaction the shared cache will be bypassed, meaning the result may not be originals from
   *     the parent but raw database rows.
   * @param unitOfWork the unitOfWork the result is being registered in.
   * @param arguments the original arguments/parameters passed to the query execution. Used by
   *     conforming
   * @param buildDirectlyFromRows If in transaction must construct a registered result from raw
   *     database rows.
   * @return the final (conformed, refreshed, wrapped) UnitOfWork query result
   */
  public Object registerResultInUnitOfWork(
      Object result,
      UnitOfWorkImpl unitOfWork,
      AbstractRecord arguments,
      boolean buildDirectlyFromRows) {
    // For bug 2612366: Conforming results in UOW extremely slow.
    // Replacing results with registered versions in the UOW is a part of
    // conforming and is now done while conforming to maximize performance.
    if (shouldConformResultsInUnitOfWork()
        || this.descriptor.shouldAlwaysConformResultsInUnitOfWork()) {
      return conformResult(result, unitOfWork, arguments, buildDirectlyFromRows);
    }

    // When building directly from rows, one of the performance benefits
    // is that we no longer have to wrap and then unwrap the originals.
    // result is just a vector, not a collection of wrapped originals.
    // Also for cursors the initial connection is automatically registered.
    if (buildDirectlyFromRows) {
      List<AbstractRecord> rows = (List<AbstractRecord>) result;
      ContainerPolicy cp = getContainerPolicy();
      int size = rows.size();
      Object clones = cp.containerInstance(size);
      for (int index = 0; index < size; index++) {
        AbstractRecord row = rows.get(index);

        // null is placed in the row collection for 1-m joining to filter duplicate rows.
        if (row != null) {
          Object clone = buildObject(row);
          cp.addInto(clone, clones, unitOfWork, row, this);
        }
      }
      return clones;
    }

    ContainerPolicy cp;
    Cursor cursor = null;

    // If the query is redirected then the collection returned might no longer
    // correspond to the original container policy.  CR#2342-S.M.
    if (getRedirector() != null) {
      cp = ContainerPolicy.buildPolicyFor(result.getClass());
    } else {
      cp = getContainerPolicy();
    }

    // In the case of cursors just register the initially read collection.
    if (cp.isCursorPolicy()) {
      cursor = (Cursor) result;
      // In nested UnitOfWork session might have been session of the parent.
      cursor.setSession(unitOfWork);
      cp = ContainerPolicy.buildPolicyFor(ClassConstants.Vector_class);
      result = cursor.getObjectCollection();
    }

    Object clones = cp.containerInstance(cp.sizeFor(result));
    AbstractSession sessionToUse = unitOfWork.getParent();
    for (Object iter = cp.iteratorFor(result); cp.hasNext(iter); ) {
      Object object = cp.next(iter, sessionToUse);
      Object clone = registerIndividualResult(object, unitOfWork, this.joinedAttributeManager);
      cp.addInto(clone, clones, unitOfWork);
    }
    if (cursor != null) {
      cursor.setObjectCollection((Vector) clones);
      return cursor;
    } else {
      return clones;
    }
  }
  /** INTERNAL: Conform the result if specified. */
  protected Object conformResult(
      Object result,
      UnitOfWorkImpl unitOfWork,
      AbstractRecord arguments,
      boolean buildDirectlyFromRows) {
    if (getSelectionCriteria() != null) {
      ExpressionBuilder builder = getSelectionCriteria().getBuilder();
      builder.setSession(unitOfWork.getRootSession(null));
      builder.setQueryClass(getReferenceClass());
    }

    // If the query is redirected then the collection returned might no longer
    // correspond to the original container policy.  CR#2342-S.M.
    ContainerPolicy cp;
    if (getRedirector() != null) {
      cp = ContainerPolicy.buildPolicyFor(result.getClass());
    } else {
      cp = getContainerPolicy();
    }

    // This code is now a great deal different...  For one, registration is done
    // as part of conforming.  Also, this should only be called if one actually
    // is conforming.
    // First scan the UnitOfWork for conforming instances.
    // This will walk through the entire cache of registered objects.
    // Let p be objects from result not in the cache.
    // Let c be objects from cache.
    // Presently p intersect c = empty set, but later p subset c.
    // By checking cache now doesConform will be called p fewer times.
    Map indexedInterimResult =
        unitOfWork.scanForConformingInstances(
            getSelectionCriteria(), getReferenceClass(), arguments, this);

    Cursor cursor = null;
    // In the case of cursors just conform/register the initially read collection.
    if (cp.isCursorPolicy()) {
      cursor = (Cursor) result;
      cp = ContainerPolicy.buildPolicyFor(ClassConstants.Vector_class);
      // In nested UnitOfWork session might have been session of the parent.
      cursor.setSession(unitOfWork);
      result = cursor.getObjectCollection();
      // for later incremental conforming...
      cursor.setInitiallyConformingIndex(indexedInterimResult);
      cursor.setSelectionCriteriaClone(getSelectionCriteria());
      cursor.setTranslationRow(arguments);
    }

    // Now conform the result from the database.
    // Remove any deleted or changed objects that no longer conform.
    // Deletes will only work for simple queries, queries with or's or anyof's may not return
    // correct results when untriggered indirection is in the model.
    Vector fromDatabase = null;

    // When building directly from rows, one of the performance benefits
    // is that we no longer have to wrap and then unwrap the originals.
    // result is just a vector, not a container of wrapped originals.
    if (buildDirectlyFromRows) {
      Vector rows = (Vector) result;
      fromDatabase = new Vector(rows.size());
      for (int i = 0; i < rows.size(); i++) {
        Object object = rows.elementAt(i);
        // null is placed in the row collection for 1-m joining to filter duplicate rows.
        if (object != null) {
          Object clone =
              conformIndividualResult(
                  object,
                  unitOfWork,
                  arguments,
                  getSelectionCriteria(),
                  indexedInterimResult,
                  buildDirectlyFromRows);
          if (clone != null) {
            fromDatabase.addElement(clone);
          }
        }
      }
    } else {
      fromDatabase = new Vector(cp.sizeFor(result));
      AbstractSession sessionToUse = unitOfWork.getParent();
      for (Object iter = cp.iteratorFor(result); cp.hasNext(iter); ) {
        Object object = cp.next(iter, sessionToUse);
        Object clone =
            conformIndividualResult(
                object,
                unitOfWork,
                arguments,
                getSelectionCriteria(),
                indexedInterimResult,
                buildDirectlyFromRows);
        if (clone != null) {
          fromDatabase.addElement(clone);
        }
      }
    }

    // Now add the unwrapped conforming instances into an appropriate container.
    // Wrapping is done automatically.
    // Make sure a vector of exactly the right size is returned.
    Object conformedResult =
        cp.containerInstance(indexedInterimResult.size() + fromDatabase.size());
    Object eachClone;
    for (Iterator enumtr = indexedInterimResult.values().iterator(); enumtr.hasNext(); ) {
      eachClone = enumtr.next();
      cp.addInto(eachClone, conformedResult, unitOfWork);
    }
    for (Enumeration enumtr = fromDatabase.elements(); enumtr.hasMoreElements(); ) {
      eachClone = enumtr.nextElement();
      cp.addInto(eachClone, conformedResult, unitOfWork);
    }

    if (cursor != null) {
      cursor.setObjectCollection((Vector) conformedResult);

      // For nested UOW must copy all in object collection to
      // initiallyConformingIndex, as some of these could have been from
      // the parent UnitOfWork.
      if (unitOfWork.isNestedUnitOfWork()) {
        for (Enumeration enumtr = cursor.getObjectCollection().elements();
            enumtr.hasMoreElements(); ) {
          Object clone = enumtr.nextElement();
          indexedInterimResult.put(clone, clone);
        }
      }
      return cursor;
    } else {
      return conformedResult;
    }
  }
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery sourceQuery,
      CacheKey cacheKey,
      AbstractSession executionSession,
      boolean isTargetProtected,
      Boolean[] wasCacheUsed)
      throws DatabaseException {
    ContainerPolicy cp = this.getContainerPolicy();

    Object fieldValue = row.getValues(this.getField());

    // BUG#2667762 there could be whitespace in the row instead of null
    if ((fieldValue == null) || (fieldValue instanceof String)) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Vector nestedRows =
        this.getDescriptor().buildNestedRowsFromFieldValue(fieldValue, executionSession);
    if (nestedRows == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Object result = null;
    if (reuseContainer) {
      Object currentObject = ((XMLRecord) row).getCurrentObject();
      Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
      result = container != null ? container : cp.containerInstance();
    } else {
      result = cp.containerInstance(nestedRows.size());
    }

    for (Enumeration stream = nestedRows.elements(); stream.hasMoreElements(); ) {
      XMLRecord nestedRow = (XMLRecord) stream.nextElement();
      Object objectToAdd;
      if (getNullPolicy().valueIsNull((Element) nestedRow.getDOM())) {
        objectToAdd = null;
      } else {
        objectToAdd =
            buildObjectFromNestedRow(
                nestedRow, joinManager, sourceQuery, executionSession, isTargetProtected);
      }
      cp.addInto(objectToAdd, result, sourceQuery.getSession());
      if (null != getContainerAccessor()) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        if (this.inverseReferenceMapping.getContainerPolicy() == null) {
          getContainerAccessor().setAttributeValueInObject(objectToAdd, currentObject);
        } else {
          Object backpointerContainer =
              getContainerAccessor().getAttributeValueFromObject(objectToAdd);
          if (backpointerContainer == null) {
            backpointerContainer =
                this.inverseReferenceMapping.getContainerPolicy().containerInstance();
            getContainerAccessor().setAttributeValueInObject(objectToAdd, backpointerContainer);
          }
          this.inverseReferenceMapping
              .getContainerPolicy()
              .addInto(currentObject, backpointerContainer, executionSession);
        }
      }
    }
    return result;
  }
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery query,
      AbstractSession executionSession) {
    ContainerPolicy cp = this.getContainerPolicy();

    Object fieldValue = row.getValues(this.getField());
    if (fieldValue == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Vector fieldValues = this.getDescriptor().buildDirectValuesFromFieldValue(fieldValue);
    if (fieldValues == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Object result = null;
    if (reuseContainer) {
      Object currentObject = ((XMLRecord) row).getCurrentObject();
      Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
      result = container != null ? container : cp.containerInstance();
    } else {
      result = cp.containerInstance(fieldValues.size());
    }

    for (Enumeration stream = fieldValues.elements(); stream.hasMoreElements(); ) {
      Object element = stream.nextElement();

      // PERF: Direct variable access.
      // Object value = row.get(field);
      // Object fieldValue = null;
      XMLUnmarshaller unmarshaller = ((XMLRecord) row).getUnmarshaller();
      if (element instanceof String) {
        if (this.isSwaRef() && (unmarshaller.getAttachmentUnmarshaller() != null)) {
          fieldValue =
              unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler((String) element);
        } else if (!this.isSwaRef()) {
          // should be base64
          byte[] bytes =
              ((XMLConversionManager)
                      executionSession.getDatasourcePlatform().getConversionManager())
                  .convertSchemaBase64ToByteArray(element);
          fieldValue = bytes;
        }
      } else {
        // this was an element, so do the XOP/SWAREF/Inline binary cases for an element
        XMLRecord record = (XMLRecord) element;
        record.setSession(executionSession);

        if ((unmarshaller.getAttachmentUnmarshaller() != null)
            && unmarshaller.getAttachmentUnmarshaller().isXOPPackage()
            && !this.isSwaRef()
            && !this.shouldInlineBinaryData()) {
          // look for the include element:
          String xpath = XMLConstants.EMPTY_STRING;

          //  need a prefix for XOP
          String prefix = null;
          NamespaceResolver descriptorResolver =
              ((XMLDescriptor) getDescriptor()).getNamespaceResolver();
          if (descriptorResolver != null) {
            prefix = descriptorResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
          }
          if (prefix == null) {
            prefix = XMLConstants.XOP_PREFIX;
          }
          NamespaceResolver tempResolver = new NamespaceResolver();
          tempResolver.put(prefix, XMLConstants.XOP_URL);
          xpath = prefix + XMLConstants.COLON + INCLUDE + "/@href";
          XMLField field = new XMLField(xpath);
          field.setNamespaceResolver(tempResolver);
          String includeValue = (String) record.get(field);
          if (element != null && includeValue != null) {
            if ((getAttributeElementClass() == ClassConstants.ABYTE)
                || (getAttributeElementClass() == ClassConstants.APBYTE)) {
              fieldValue =
                  unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray(includeValue);
            } else {
              fieldValue =
                  unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(includeValue);
            }
          } else {
            // If we didn't find the Include element, check for inline
            fieldValue = record.get(XMLConstants.TEXT);
            // should be a base64 string
            fieldValue =
                ((XMLConversionManager)
                        executionSession.getDatasourcePlatform().getConversionManager())
                    .convertSchemaBase64ToByteArray(fieldValue);
          }
        } else if ((unmarshaller.getAttachmentUnmarshaller() != null) && isSwaRef()) {
          String refValue = (String) record.get(XMLConstants.TEXT);
          if (refValue != null) {
            fieldValue =
                unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(refValue);
          }
        } else {
          fieldValue = record.get(XMLConstants.TEXT);
          // should be a base64 string
          fieldValue =
              ((XMLConversionManager)
                      executionSession.getDatasourcePlatform().getConversionManager())
                  .convertSchemaBase64ToByteArray(fieldValue);
        }
      }
      Object attributeValue = fieldValue;
      if (getValueConverter() != null) {
        if (getValueConverter() instanceof XMLConverter) {
          attributeValue =
              ((XMLConverter) getValueConverter())
                  .convertDataValueToObjectValue(fieldValue, executionSession, unmarshaller);
        } else {
          attributeValue =
              getValueConverter().convertDataValueToObjectValue(fieldValue, executionSession);
        }
      }

      cp.addInto(attributeValue, result, query.getSession());
    }
    return result;
  }
  /**
   * INTERNAL: Build and return the nested rows from the specified field value. This method allows
   * the field value to be an ARRAY containing other structures such as arrays or Struct, or direct
   * values.
   */
  public static Object buildContainerFromArray(
      Array fieldValue, ObjectRelationalDatabaseField arrayField, AbstractSession session)
      throws DatabaseException {
    if (arrayField.getType() == null) {
      return fieldValue;
    }
    Object[] objects = null;
    try {
      objects = (Object[]) fieldValue.getArray();
    } catch (java.sql.SQLException ex) {
      throw DatabaseException.sqlException(ex, session, false);
    }
    if (objects == null) {
      return null;
    }

    boolean isNestedStructure = false;
    ObjectRelationalDataTypeDescriptor ord = null;
    DatabaseField nestedType = null;
    if (arrayField != null) {
      nestedType = arrayField.getNestedTypeField();
      if ((nestedType != null) && nestedType.getSqlType() == Types.STRUCT) {
        ClassDescriptor descriptor = session.getDescriptor(nestedType.getType());
        if ((descriptor != null) && (descriptor.isObjectRelationalDataTypeDescriptor())) {
          // this is used to convert non-null objects passed through stored procedures and custom
          // SQL to structs
          ord = (ObjectRelationalDataTypeDescriptor) descriptor;
        }
      } else if ((nestedType != null) && (nestedType instanceof ObjectRelationalDatabaseField)) {
        isNestedStructure = true;
      }
    }
    // handle ARRAY conversions
    ReadObjectQuery query = new ReadObjectQuery();
    query.setSession(session);
    ContainerPolicy cp = ContainerPolicy.buildPolicyFor(arrayField.getType());
    Object container = cp.containerInstance(objects.length);
    for (int i = 0; i < objects.length; i++) {
      Object arrayValue = objects[i];
      if (arrayValue == null) {
        return null;
      }
      if (ord != null) {
        AbstractRecord nestedRow = ord.buildRowFromStructure((Struct) arrayValue);
        ClassDescriptor descriptor = ord;
        if (descriptor.hasInheritance()) {
          Class newElementClass =
              descriptor.getInheritancePolicy().classFromRow(nestedRow, session);
          if (!descriptor.getJavaClass().equals(newElementClass)) {
            descriptor = session.getDescriptor(newElementClass);
            if (descriptor == null) {
              descriptor = ord;
            }
          }
        }
        arrayValue = descriptor.getObjectBuilder().buildNewInstance();
        descriptor
            .getObjectBuilder()
            .buildAttributesIntoObject(arrayValue, nestedRow, query, null, false);
      } else if (isNestedStructure && (arrayValue instanceof Array)) {
        arrayValue =
            buildContainerFromArray(
                (Array) arrayValue, (ObjectRelationalDatabaseField) nestedType, session);
      }

      cp.addInto(arrayValue, container, session);
    }
    return container;
  }