/**
   * INTERNAL Return true if it uses a cast class and query is downcasting. It will look into
   * inheritance hierarchy of the root descriptor.
   */
  public boolean isDowncast(ClassDescriptor rootDescriptor, AbstractSession session) {
    if (castClass == null) {
      return false;
    }

    if (rootDescriptor.getJavaClass() == castClass) {
      return false;
    }

    ClassDescriptor castDescriptor = session.getClassDescriptor(castClass);

    if (castDescriptor == null) {
      throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression());
    }
    if (castDescriptor.getInheritancePolicy() == null) {
      throw QueryException.castMustUseInheritance(getBaseExpression());
    }
    ClassDescriptor parentDescriptor = castDescriptor.getInheritancePolicy().getParentDescriptor();
    while (parentDescriptor != null) {
      if (parentDescriptor == rootDescriptor) {
        return true;
      }
      parentDescriptor = parentDescriptor.getInheritancePolicy().getParentDescriptor();
    }

    throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression());
  }
  /**
   * INTERNAL Return the descriptor which contains this query key, look in the inheritance hierarchy
   * of rootDescriptor for the descriptor.
   */
  public ClassDescriptor convertToCastDescriptor(
      ClassDescriptor rootDescriptor, AbstractSession session) {
    if (castClass == null || rootDescriptor == null || rootDescriptor.getJavaClass() == castClass) {
      return rootDescriptor;
    }

    ClassDescriptor castDescriptor = session.getClassDescriptor(castClass);

    if (castDescriptor == null) {
      throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression());
    }
    if (!castDescriptor.hasInheritance()) {
      throw QueryException.castMustUseInheritance(getBaseExpression());
    }
    ClassDescriptor parentDescriptor = castDescriptor.getInheritancePolicy().getParentDescriptor();
    while (parentDescriptor != null) {
      if (parentDescriptor == rootDescriptor) {
        return castDescriptor;
      }
      parentDescriptor = parentDescriptor.getInheritancePolicy().getParentDescriptor();
    }

    ClassDescriptor childDescriptor = rootDescriptor;
    while (childDescriptor != null) {
      if (childDescriptor == castDescriptor) {
        return rootDescriptor;
      }
      childDescriptor = childDescriptor.getInheritancePolicy().getParentDescriptor();
    }

    throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression());
  }
  /**
   * INTERNAL - Return the descriptor which contains this query key, look in the inheritance
   * hierarchy of rootDescriptor for the descriptor. Does not set the descriptor, only returns it.
   */
  public ClassDescriptor convertToCastDescriptor(
      ClassDescriptor rootDescriptor, AbstractSession session) {
    isDowncast = Boolean.FALSE;
    if (castClass == null || rootDescriptor == null || rootDescriptor.getJavaClass() == castClass) {
      return rootDescriptor;
    }

    ClassDescriptor castDescriptor = session.getClassDescriptor(castClass);

    if (castDescriptor == null) {
      throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression());
    }
    if (!castDescriptor.hasInheritance()) {
      throw QueryException.castMustUseInheritance(getBaseExpression());
    }
    ClassDescriptor parentDescriptor = castDescriptor.getInheritancePolicy().getParentDescriptor();
    while (parentDescriptor != null) {
      if (parentDescriptor == rootDescriptor) {
        isDowncast = Boolean.TRUE;
        return castDescriptor;
      }
      parentDescriptor = parentDescriptor.getInheritancePolicy().getParentDescriptor();
    }
    // is there value casting an emp to person in a query?
    ClassDescriptor childDescriptor = rootDescriptor;
    while (childDescriptor != null) {
      if (childDescriptor == castDescriptor) {
        return rootDescriptor;
      }
      childDescriptor = childDescriptor.getInheritancePolicy().getParentDescriptor();
    }

    throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression());
  }
 /** INTERNAL: */
 @Override
 public List<DatabaseField> getSelectionFields(ReadQuery query) {
   if (getDescriptor() == null) {
     DatabaseMapping mapping = getMapping();
     if (mapping != null) {
       return mapping.getSelectFields();
     }
     return new ArrayList<DatabaseField>(0);
   }
   if (descriptor.hasInheritance()
           && descriptor.getInheritancePolicy().shouldReadSubclasses()
           && (!descriptor.getInheritancePolicy().hasMultipleTableChild())
       || shouldUseOuterJoinForMultitableInheritance()) {
     // return all fields because we can.
     if (query != null && query.isObjectLevelReadQuery()) {
       return descriptor.getAllSelectionFields((ObjectLevelReadQuery) query);
     } else {
       return descriptor.getAllSelectionFields();
     }
   } else {
     if (query != null && query.isObjectLevelReadQuery()) {
       return descriptor.getSelectionFields((ObjectLevelReadQuery) query);
     } else {
       return descriptor.getSelectionFields();
     }
   }
 }
 /**
  * INTERNAL: this returns a single expression to represent the join from the main table to all
  * child descriptor tables Only if outer joins should be printed in the where clause
  *
  * @return Expression
  */
 public Expression getTreatCriteria() {
   if (getDescriptor() == null) {
     return null;
   }
   // need to build this using just the multiple tables on this descriptor not included in the
   // parent's join expression
   Expression criteria = null;
   if (getSession().getPlatform().shouldPrintOuterJoinInWhereClause()) {
     Vector tables = getDescriptor().getTables(); // This child's tables
     ClassDescriptor parentDescriptor = this.typeExpressionBase.getDescriptor();
     int tablesSize = tables.size();
     if (parentDescriptor.hasInheritance()
         && parentDescriptor.getInheritancePolicy().hasMultipleTableChild()) {
       // look up the joins from the parent descriptor to our tables.
       for (int i = 0; i < tablesSize; i++) {
         DatabaseTable table = (DatabaseTable) tables.elementAt(i);
         Expression joinExpression =
             parentDescriptor.getInheritancePolicy().getChildrenTablesJoinExpressions().get(table);
         // Some of our tables might be the in our parent as well, so ignore the lack of a
         // joinExpression
         if (joinExpression != null) {
           joinExpression = this.baseExpression.twist(joinExpression, this);
           if (shouldUseOuterJoin()) {
             joinExpression = joinExpression.convertToUseOuterJoin();
           }
           criteria = joinExpression.and(criteria);
         }
       }
     }
   }
   return criteria;
 }
 /** Return any tables that are defined by this expression (and not its base). */
 @Override
 public List<DatabaseTable> getOwnedTables() {
   ClassDescriptor descriptor = getDescriptor();
   List<DatabaseTable> tables = null;
   if (descriptor == null) {
     List additionalTables = getAdditionalTables();
     if (additionalTables == null) {
       return null;
     } else {
       return new ArrayList(additionalTables);
     }
   } else if (descriptor.isAggregateDescriptor()) {
     return null;
   } else if ((descriptor.getHistoryPolicy() != null)
       && (getAsOfClause() != null && getAsOfClause().getValue() != null)) {
     tables = descriptor.getHistoryPolicy().getHistoricalTables();
   } else if (isUsingOuterJoinForMultitableInheritance()) {
     tables = descriptor.getInheritancePolicy().getAllTables();
   } else {
     tables = descriptor.getTables();
   }
   List additionalTables = getAdditionalTables();
   if (additionalTables != null) {
     tables = new Vector(tables);
     Helper.addAllUniqueToList(tables, additionalTables);
     return tables;
   }
   return tables;
 }
 protected static void afterAddDescriptors(Session session, TestSystem aTestSystem) {
   if (aTestSystem instanceof InheritanceSystem) {
     // For using read all subclasses views.
     org.eclipse.persistence.internal.databaseaccess.DatabasePlatform platform =
         session.getLogin().getPlatform();
     if (platform.isOracle() || platform.isSybase() || platform.isSQLAnywhere()) {
       ClassDescriptor computerDescriptor = session.getClassDescriptor(Computer.class);
       ClassDescriptor vehicleDescriptor = session.getClassDescriptor(Vehicle.class);
       if (computerDescriptor.getInheritancePolicy().requiresMultipleTableSubclassRead()) {
         computerDescriptor.getInheritancePolicy().setReadAllSubclassesViewName("AllComputers");
       }
       if (vehicleDescriptor.getInheritancePolicy().requiresMultipleTableSubclassRead()) {
         vehicleDescriptor.getInheritancePolicy().setReadAllSubclassesViewName("AllVehicles");
       }
     }
   }
 }
 /**
  * Return whether the reference objects must be deleted one by one, as opposed to with a single
  * DELETE statement.
  */
 protected boolean mustDeleteReferenceObjectsOneByOne() {
   ClassDescriptor referenceDescriptor = this.getReferenceDescriptor();
   return referenceDescriptor.hasDependencyOnParts()
       || referenceDescriptor.usesOptimisticLocking()
       || (referenceDescriptor.hasInheritance()
           && referenceDescriptor.getInheritancePolicy().shouldReadSubclasses())
       || referenceDescriptor.hasMultipleTables();
 }
  protected void initializeWithTableNames(Session session, Collection tableNames) {
    tableNameToClass = new Hashtable(tableNames.size());
    tableNameToPkFieldNames = new Hashtable(tableNames.size());

    // pkFieldVectors cached here to avoid calculating it more than once per class
    Hashtable classToPkFieldNames = new Hashtable();
    // loop through the descriptors to fill out tableNameToClass and tableNameToPkFieldNames
    Iterator descriptors = session.getDescriptors().values().iterator();
    while (descriptors.hasNext() && !tableNames.isEmpty()) {
      ClassDescriptor desc = (ClassDescriptor) descriptors.next();

      // Create a Vector containing names of all tables mapped to the descriptor
      Vector descTableNames = desc.getTableNames();

      // bypass descriptors with no tables
      if (descTableNames.isEmpty()) {
        continue;
      }

      // Remove schema names (if any) converting "SCHEMA_NAME.TABLE_NAME" to "TABLE_NAME"
      removePrefixFromDatabaseObjectNames(descTableNames);

      // handle inheritance: table name should be mapped to the base mapped class
      Class baseClass = desc.getJavaClass();
      while (desc.isChildDescriptor()) {
        desc = session.getDescriptor(desc.getInheritancePolicy().getParentClass());
        baseClass = desc.getJavaClass();
      }

      Iterator it = tableNames.iterator();
      while (it.hasNext()) {
        // for each tableName specified by the user
        String tableName = (String) it.next();
        // verify whether the descriptor maps a table with the same name
        if (descTableNames.contains(tableName)) {
          // map the table name to the baseClass corresponding to the descriptor
          tableNameToClass.put(tableName, baseClass);

          // try to obtain cached pkFieldNames Vector corresponding to baseClass
          Vector pkFieldNames = (Vector) classToPkFieldNames.get(baseClass);
          if (pkFieldNames == null) {
            // Create a Vector containing names of all primary key fields
            pkFieldNames = desc.getPrimaryKeyFieldNames();
            // Remove table name converting from "TABLE_NAME.FIELD_NAME" to "FIELD_NAME"
            removePrefixFromDatabaseObjectNames(pkFieldNames);
            // cache pkFieldNames Vector corresponding to baseClass
            classToPkFieldNames.put(baseClass, pkFieldNames);
          }
          // map the table name to the Vector of names of primary key fields.
          tableNameToPkFieldNames.put(tableName, pkFieldNames);

          // the table name is mapped - remove it from the list of table names to be mapped.
          it.remove();
        }
      }
    }
  }
 /**
  * INTERNAL: Not to be confused with the public getField(String) This returns a collection of all
  * fields associated with this object. Really only applies to query keys representing an object or
  * to expression builders.
  */
 @Override
 public Vector getFields() {
   if (getDescriptor() == null) {
     DatabaseMapping mapping = getMapping();
     if (mapping != null) {
       return mapping.getSelectFields();
     }
     return new NonSynchronizedVector(0);
   }
   if (descriptor.hasInheritance()
           && descriptor.getInheritancePolicy().shouldReadSubclasses()
           && (!descriptor.getInheritancePolicy().hasMultipleTableChild())
       || shouldUseOuterJoinForMultitableInheritance()) {
     // return all fields because we can.
     return descriptor.getAllFields();
   } else {
     return descriptor.getFields();
   }
 }
  /**
   * INTERNAL: Used in case outer joins should be printed in FROM clause. Each of the additional
   * tables mapped to expressions that joins it.
   */
  public Map additionalTreatExpressionCriteriaMap() {
    if (getDescriptor() == null) {
      return null;
    }
    int tableSize = 0;
    HashMap tablesJoinExpressions = new HashMap();

    ClassDescriptor parentDescriptor = this.typeExpressionBase.getDescriptor();

    // outerjoin our parent->child tables
    if (parentDescriptor.hasInheritance()
        && parentDescriptor.getInheritancePolicy().hasMultipleTableChild()) {
      Vector tables = getDescriptor().getTables(); // All this child's tables
      tableSize = tables.size();
      // look up the joins from the parent descriptor to our tables.
      for (int i = 0; i < tableSize; i++) {
        DatabaseTable table = (DatabaseTable) tables.elementAt(i);
        Expression joinExpression =
            parentDescriptor.getInheritancePolicy().getChildrenTablesJoinExpressions().get(table);
        // Some of our tables might be the in our parent as well, so ignore the lack of a
        // joinExpression
        if (joinExpression != null) {
          joinExpression = this.baseExpression.twist(joinExpression, this);
          tablesJoinExpressions.put(table, joinExpression);
        }
      }
    }

    if (isUsingOuterJoinForMultitableInheritance()) {
      List childrenTables = getDescriptor().getInheritancePolicy().getChildrenTables();
      tableSize = childrenTables.size();
      for (int i = 0; i < tableSize; i++) {
        DatabaseTable table = (DatabaseTable) childrenTables.get(i);
        Expression joinExpression =
            getDescriptor().getInheritancePolicy().getChildrenTablesJoinExpressions().get(table);
        joinExpression = this.baseExpression.twist(joinExpression, this);
        tablesJoinExpressions.put(table, joinExpression);
      }
    }

    return tablesJoinExpressions;
  }
  protected void setup() {
    getSession().getIdentityMapAccessor().initializeAllIdentityMaps();

    descriptorToModify =
        project
            .getDescriptors()
            .get(org.eclipse.persistence.testing.models.employee.domain.Project.class);
    descriptorToModify
        .getInheritancePolicy()
        .setReadAllSubclassesViewName("Testing  getReadAllSubclassesView ()");
  }
 public static Hashtable buildInheritanceHierarchyTree(Project project) {
   Map descriptors = project.getDescriptors();
   Hashtable hierarchyTree = new Hashtable(descriptors.size());
   for (Iterator descriptorIterator = descriptors.values().iterator();
       descriptorIterator.hasNext(); ) {
     ClassDescriptor descriptor = (ClassDescriptor) descriptorIterator.next();
     String className = descriptor.getJavaClassName();
     if (className == null) {
       className = descriptor.getJavaClass().getName();
     }
     HierarchyNode node = getNodeForClass(className, hierarchyTree);
     if (descriptor.hasInheritance()
         && (descriptor.getInheritancePolicy().getParentClassName() != null)) {
       HierarchyNode parentNode =
           getNodeForClass(descriptor.getInheritancePolicy().getParentClassName(), hierarchyTree);
       node.setParent(parentNode);
     }
   }
   return hierarchyTree;
 }
  public void addDescriptors(DatabaseSession session) {
    // Oracle has bug in outjoins that require outerjoin of inheritance type.
    // This should really be handled by the platform during expression normalization...
    // Id for Entomologist can be negative (millis cast to int wraps...)
    project.getDescriptor(Entomologist.class).setIdValidation(IdValidation.NONE);

    session.addDescriptors(project);

    // For using read all subclasses views.
    DatabasePlatform platform = session.getLogin().getPlatform();
    if (platform.isOracle() || platform.isSybase()) {
      ClassDescriptor computerDescriptor = session.getDescriptor(Computer.class);
      ClassDescriptor vehicleDescriptor = session.getDescriptor(Vehicle.class);
      computerDescriptor.getInheritancePolicy().setReadAllSubclassesViewName("AllComputers");
      vehicleDescriptor.getInheritancePolicy().setReadAllSubclassesViewName("AllVehicles");
    }

    // Enable outer-join on AnimalMatt hierarchy.
    session
        .getDescriptor(Animal_Matt.class)
        .getInheritancePolicy()
        .setShouldOuterJoinSubclasses(true);
  }
  /**
   * INTERNAL: much like getOwnedTables(), this gets the tables represented from the descriptor.
   * Difference is this only returns local tables for the child casted descriptor, and excludes
   * tables owned by the parent descriptor
   */
  public List<DatabaseTable> getOwnedSubTables() {
    ClassDescriptor parentDescriptor = this.typeExpressionBase.getDescriptor();
    Vector<DatabaseTable> childTables = new Vector(2);
    if (parentDescriptor.hasInheritance()
        && parentDescriptor.getInheritancePolicy().hasMultipleTableChild()) {
      List parentTables = typeExpressionBase.getOwnedTables();
      // All tables for this child, including parent tables
      Vector<DatabaseTable> tables = getDescriptor().getTables();
      for (DatabaseTable table : tables) {
        if (!parentTables.contains(table)) {
          childTables.add(table);
        }
      }
    }

    return childTables;
  }
 protected Object readObjectFromRow(Session session, ClassDescriptor desc, Record row) {
   if (desc.hasInheritance()) {
     Class newClass =
         desc.getInheritancePolicy().classFromRow((DatabaseRecord) row, (AbstractSession) session);
     desc = session.getClassDescriptor(newClass);
   }
   Object object = desc.getObjectBuilder().buildNewInstance();
   ReadObjectQuery query = new ReadObjectQuery();
   query.setSession((AbstractSession) session);
   for (Enumeration mappings = desc.getMappings().elements(); mappings.hasMoreElements(); ) {
     DatabaseMapping mapping = (DatabaseMapping) mappings.nextElement();
     mapping.readFromRowIntoObject(
         (DatabaseRecord) row,
         query.getJoinedAttributeManager(),
         object,
         null,
         query,
         query.getSession(),
         true);
   }
   return object;
 }
Beispiel #17
0
 public void customize(ClassDescriptor descriptor) {
   descriptor.getInheritancePolicy().setShouldOuterJoinSubclasses(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;
  }
  public Object buildObjectFromNestedRow(
      AbstractRecord nestedRow,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery sourceQuery,
      AbstractSession executionSession,
      boolean isTargetProtected) {
    Object objectToAdd = null;
    ClassDescriptor aDescriptor = getReferenceDescriptor((DOMRecord) nestedRow);

    if (aDescriptor == null) {
      if ((getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT)
          || (getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT)) {
        XMLPlatformFactory.getInstance()
            .getXMLPlatform()
            .namespaceQualifyFragment((Element) ((DOMRecord) nestedRow).getDOM());
        objectToAdd = ((DOMRecord) nestedRow).getDOM();
        convertDataValueToObjectValue(
            objectToAdd, executionSession, ((XMLRecord) nestedRow).getUnmarshaller());
        // simple case
        objectToAdd = convertToSimpleTypeIfPresent(objectToAdd, nestedRow, executionSession);
      } else {
        NodeList children = ((Element) ((DOMRecord) nestedRow).getDOM()).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
          Node nextNode = children.item(i);
          if (nextNode.getNodeType() == nextNode.ELEMENT_NODE) {
            // complex child
            String type =
                ((Element) ((DOMRecord) nestedRow).getDOM())
                    .getAttributeNS(
                        javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
                        XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
            if (type != null && type.length() > 0) {
              throw XMLMarshalException.unknownXsiTypeValue(type, (Mapping) this);
            } else {
              throw XMLMarshalException.noDescriptorFound((Mapping) this);
            }
          }
        }
        // simple case
        objectToAdd = convertToSimpleTypeIfPresent(objectToAdd, nestedRow, executionSession);
      }
    } else {
      if (aDescriptor.hasInheritance()) {
        Class newElementClass =
            aDescriptor.getInheritancePolicy().classFromRow(nestedRow, executionSession);
        if (newElementClass == null) {
          // no xsi:type attribute - look for type indicator on the field
          QName leafElementType = ((XMLField) getField()).getLeafElementType();
          if (leafElementType != null) {
            XPathQName leafElementXPathQName =
                new XPathQName(leafElementType, ((XMLRecord) nestedRow).isNamespaceAware());
            Object indicator =
                aDescriptor
                    .getInheritancePolicy()
                    .getClassIndicatorMapping()
                    .get(leafElementXPathQName);
            if (indicator != null) {
              newElementClass = (Class) indicator;
            }
          }
        }
        if (newElementClass != null) {
          aDescriptor = this.getReferenceDescriptor(newElementClass, executionSession);
        } else {
          // since there is no xsi:type attribute or leaf element type set,
          // use the reference descriptor -  make sure it is non-abstract
          if (Modifier.isAbstract(aDescriptor.getJavaClass().getModifiers())) {
            // throw an exception
            throw DescriptorException.missingClassIndicatorField(
                nestedRow, aDescriptor.getInheritancePolicy().getDescriptor());
          }
        }
      }

      // Object element
      objectToAdd =
          buildCompositeObject(
              aDescriptor, nestedRow, sourceQuery, null, joinManager, executionSession);
      objectToAdd =
          convertDataValueToObjectValue(
              objectToAdd, executionSession, ((XMLRecord) nestedRow).getUnmarshaller());
    }
    return objectToAdd;
  }
  protected void onConnectAllSequences() {
    connectedSequences = new Vector();
    boolean shouldUseTransaction = false;
    boolean shouldUsePreallocation = false;
    boolean shouldAcquireValueAfterInsert = false;
    Iterator descriptors = getOwnerSession().getDescriptors().values().iterator();
    while (descriptors.hasNext()) {
      ClassDescriptor descriptor = (ClassDescriptor) descriptors.next();
      // Find root sequence, because inheritance needs to be resolved here.
      // TODO: The way we initialize sequencing needs to be in line with descriptor init.
      ClassDescriptor parentDescriptor = descriptor;
      while (!parentDescriptor.usesSequenceNumbers() && parentDescriptor.isChildDescriptor()) {
        ClassDescriptor newDescriptor =
            getOwnerSession()
                .getDescriptor(parentDescriptor.getInheritancePolicy().getParentClass());
        // Avoid issue with error cases of self parent, or null parent.
        if ((newDescriptor == null) || (newDescriptor == parentDescriptor)) {
          break;
        }
        parentDescriptor = newDescriptor;
      }
      if (!parentDescriptor.usesSequenceNumbers()) {
        continue;
      }
      String seqName = parentDescriptor.getSequenceNumberName();
      Sequence sequence = getSequence(seqName);
      if (sequence == null) {
        sequence = new DefaultSequence(seqName);
        getOwnerSession().getDatasourcePlatform().addSequence(sequence);
      }
      // PERF: Initialize the sequence, this avoid having to look it up every time.
      descriptor.setSequence(sequence);
      if (connectedSequences.contains(sequence)) {
        continue;
      }
      try {
        if (sequence instanceof DefaultSequence
            && !connectedSequences.contains(getDefaultSequence())) {
          getDefaultSequence().onConnect(getOwnerSession().getDatasourcePlatform());
          connectedSequences.add(0, getDefaultSequence());
          shouldUseTransaction |= getDefaultSequence().shouldUseTransaction();
          shouldUsePreallocation |= getDefaultSequence().shouldUsePreallocation();
          shouldAcquireValueAfterInsert |= getDefaultSequence().shouldAcquireValueAfterInsert();
        }
        sequence.onConnect(getOwnerSession().getDatasourcePlatform());
        connectedSequences.addElement(sequence);
        shouldUseTransaction |= sequence.shouldUseTransaction();
        shouldUsePreallocation |= sequence.shouldUsePreallocation();
        shouldAcquireValueAfterInsert |= sequence.shouldAcquireValueAfterInsert();
      } catch (RuntimeException ex) {
        // defaultSequence has to disconnect the last
        for (int i = connectedSequences.size() - 1; i >= 0; i--) {
          try {
            Sequence sequenceToDisconnect = (Sequence) connectedSequences.elementAt(i);
            sequenceToDisconnect.onDisconnect(getOwnerSession().getDatasourcePlatform());
          } catch (RuntimeException ex2) {
            // ignore
          }
        }
        connectedSequences = null;
        throw ex;
      }
    }

    if (shouldAcquireValueAfterInsert && !shouldUsePreallocation) {
      whenShouldAcquireValueForAll = AFTER_INSERT;
    } else if (!shouldAcquireValueAfterInsert && shouldUsePreallocation) {
      whenShouldAcquireValueForAll = BEFORE_INSERT;
    }
    atLeastOneSequenceShouldUseTransaction = shouldUseTransaction;
    atLeastOneSequenceShouldUsePreallocation = shouldUsePreallocation;
  }