/** INTERNAL: Initialize the mapping. */
  @Override
  public void initialize(AbstractSession session) throws DescriptorException {
    super.initialize(session);
    if ((getStructureName() == null) || getStructureName().length() == 0) {
      throw DescriptorException.structureNameNotSetInMapping(this);
    }

    // For bug 2730536 convert the field to be an ObjectRelationalDatabaseField.
    ObjectRelationalDatabaseField field = (ObjectRelationalDatabaseField) getField();
    field.setSqlType(java.sql.Types.ARRAY);
    field.setSqlTypeName(getStructureName());

    // May require native connection in WLS to avoid wrapping wrapped.
    getDescriptor().setIsNativeConnectionRequired(true);
  }
  /** INTERNAL: Initialize the mapping. */
  public void initialize(AbstractSession session) throws DescriptorException {
    setReferenceDescriptor(session.getDescriptor(getReferenceClass()));

    if (referenceDescriptor == null) {
      throw DescriptorException.descriptorIsMissing(getReferenceClass().getName(), this);
    }

    // For bug 2730536 convert the field to be an ObjectRelationalDatabaseField.
    ObjectRelationalDatabaseField field = (ObjectRelationalDatabaseField) getField();
    field.setSqlType(java.sql.Types.REF);
    if (referenceDescriptor instanceof ObjectRelationalDataTypeDescriptor) {
      field.setSqlTypeName(
          ((ObjectRelationalDataTypeDescriptor) referenceDescriptor).getStructureName());
    }

    setField(getDescriptor().buildField(getField()));
    setFields(collectFields());

    // Ref mapping requires native connection in WLS as the Ref is wrapped.
    getDescriptor().setIsNativeConnectionRequired(true);
  }
  /**
   * 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;
  }