Program: 66925
Project: 66925
Buggy source:
/** 
 * INTERNAL: Initialize the mappings as a separate step. This is done as a separate step to ensure that inheritance has been first resolved.
 */
public void initialize(AbstractSession session) throws DescriptorException {
  if (getHistoryPolicy() != null) {
    session.getProject().setHasGenericHistorySupport(true);
  }
  if (isIsolated()) {
    session.getProject().setHasIsolatedClasses(true);
  }
  if (!shouldIsolateObjectsInUnitOfWork() && !shouldBeReadOnly()) {
    session.getProject().setHasNonIsolatedUOWClasses(true);
  }
  if (isInitialized(INITIALIZED) || isInvalid()) {
    return;
  }
  setInitializationStage(INITIALIZED);
  if (isChildDescriptor()) {
    getInheritancePolicy().getParentDescriptor().initialize(session);
    if (getInheritancePolicy().getParentDescriptor().isIsolated()) {
      setIsIsolated(true);
    }
    getInheritancePolicy().initializeOptimisticLocking();
  }
  if (shouldOrderMappings()) {
    Vector mappings=getMappings();
    Object[] mappingsArray=new Object[mappings.size()];
    for (int index=0; index < mappings.size(); index++) {
      mappingsArray[index]=mappings.elementAt(index);
    }
    Arrays.sort(mappingsArray,new MappingCompare());
    mappings=NonSynchronizedVector.newInstance(mappingsArray.length);
    for (int index=0; index < mappingsArray.length; index++) {
      mappings.addElement(mappingsArray[index]);
    }
    setMappings(mappings);
  }
  for (Enumeration mappingsEnum=getMappings().elements(); mappingsEnum.hasMoreElements(); ) {
    DatabaseMapping mapping=(DatabaseMapping)mappingsEnum.nextElement();
    validateMappingType(mapping);
    mapping.initialize(session);
    if (mapping.isLockableMapping()) {
      getLockableMappings().add(mapping);
    }
    if ((mapping.isForeignReferenceMapping()) && (((ForeignReferenceMapping)mapping).getIndirectionPolicy() instanceof ProxyIndirectionPolicy)) {
      session.getProject().setHasProxyIndirection(true);
    }
    if ((usesOptimisticLocking() && getOptimisticLockingPolicy().isCascaded()) || hasCascadeLockingPolicies()) {
      prepareCascadeLockingPolicy(mapping);
    }
    if (mapping.isDerivedIdMapping()) {
      this.hasDerivedId=true;
    }
    Helper.addAllUniqueToVector(getFields(),mapping.getFields());
  }
  if (hasMappingsPostCalculateChangesOnDeleted()) {
    session.getProject().setHasMappingsPostCalculateChangesOnDeleted(true);
  }
  if (!isAggregateDescriptor()) {
    if (!isChildDescriptor()) {
      if (usesOptimisticLocking()) {
        getOptimisticLockingPolicy().initializeProperties();
      }
    }
  }
  for (Iterator queryKeys=getQueryKeys().values().iterator(); queryKeys.hasNext(); ) {
    QueryKey queryKey=(QueryKey)queryKeys.next();
    queryKey.initialize(this);
  }
  if (hasInheritance()) {
    getInheritancePolicy().initialize(session);
    if (getInheritancePolicy().isChildDescriptor()) {
      for (Iterator iterator=getInheritancePolicy().getParentDescriptor().getMappings().iterator(); iterator.hasNext(); ) {
        DatabaseMapping mapping=(DatabaseMapping)iterator.next();
        if (mapping.isAggregateObjectMapping() || ((mapping.isForeignReferenceMapping() && (!mapping.isDirectCollectionMapping())) && (!((ForeignReferenceMapping)mapping).usesIndirection()))) {
          getLockableMappings().add(mapping);
        }
        if (mapping.isDerivedIdMapping()) {
          this.hasDerivedId=true;
        }
      }
    }
  }
  if (hasInheritance() && shouldOrderMappings()) {
    Vector mappings=getMappings();
    Object[] mappingsArray=new Object[mappings.size()];
    for (int index=0; index < mappings.size(); index++) {
      mappingsArray[index]=mappings.elementAt(index);
    }
    Arrays.sort(mappingsArray,new MappingCompare());
    mappings=NonSynchronizedVector.newInstance(mappingsArray.length);
    for (int index=0; index < mappingsArray.length; index++) {
      mappings.addElement(mappingsArray[index]);
    }
    setMappings(mappings);
  }
  setAllFields((Vector)getFields().clone());
  getObjectBuilder().initialize(session);
  if (shouldOrderMappings()) {
    for (int index=getObjectBuilder().getPrimaryKeyMappings().size() - 1; index >= 0; index--) {
      DatabaseMapping mapping=getObjectBuilder().getPrimaryKeyMappings().get(index);
      if ((mapping != null) && mapping.isDirectToFieldMapping()) {
        getMappings().remove(mapping);
        getMappings().add(0,mapping);
        DatabaseField field=((AbstractDirectMapping)mapping).getField();
        getFields().remove(field);
        getFields().add(0,field);
        getAllFields().remove(field);
        getAllFields().add(0,field);
      }
    }
  }
  if (usesOptimisticLocking() && (!isChildDescriptor())) {
    getOptimisticLockingPolicy().initialize(session);
  }
  if (hasInterfacePolicy() || isDescriptorForInterface()) {
    interfaceInitialization(session);
  }
  if (hasWrapperPolicy()) {
    getWrapperPolicy().initialize(session);
  }
  if (hasReturningPolicy()) {
    getReturningPolicy().initialize(session);
  }
  getQueryManager().initialize(session);
  getEventManager().initialize(session);
  getCopyPolicy().initialize(session);
  getInstantiationPolicy().initialize(session);
  if (getHistoryPolicy() != null) {
    getHistoryPolicy().initialize(session);
  }
 else   if (hasInheritance()) {
    ClassDescriptor parentDescriptor=getInheritancePolicy().getParentDescriptor();
    if ((parentDescriptor != null) && (parentDescriptor.getHistoryPolicy() != null)) {
      setHistoryPolicy((HistoryPolicy)parentDescriptor.getHistoryPolicy().clone());
    }
  }
  if (this.getCMPPolicy() != null) {
    this.getCMPPolicy().initialize(this,session);
  }
  if (hasFetchGroupManager()) {
    getFetchGroupManager().initialize(session);
  }
  if ((getObjectChangePolicyInternal() == null) && (ChangeTracker.class.isAssignableFrom(getJavaClass()))) {
    if (Arrays.asList(getJavaClass().getInterfaces()).contains(PersistenceWeavedChangeTracking.class)) {
      if (supportsChangeTracking(session.getProject())) {
        setObjectChangePolicy(new AttributeChangeTrackingPolicy());
      }
    }
  }
  getObjectChangePolicy().initialize(session,this);
  if (getUnitOfWorkCacheIsolationLevel() == UNDEFINED_ISOLATATION) {
    if (isIsolated()) {
      setUnitOfWorkCacheIsolationLevel(ISOLATE_CACHE_ALWAYS);
    }
 else {
      setUnitOfWorkCacheIsolationLevel(ISOLATE_NEW_DATA_AFTER_TRANSACTION);
    }
  }
  if (getIdValidation() == null) {
    if (getPrimaryKeyFields().size() > 1) {
      setIdValidation(IdValidation.NULL);
    }
 else {
      setIdValidation(IdValidation.ZERO);
    }
  }
  if (this.defaultReadAllQueryRedirector == null) {
    this.defaultReadAllQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultReadObjectQueryRedirector == null) {
    this.defaultReadObjectQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultReportQueryRedirector == null) {
    this.defaultReportQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultInsertObjectQueryRedirector == null) {
    this.defaultInsertObjectQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultUpdateObjectQueryRedirector == null) {
    this.defaultUpdateObjectQueryRedirector=this.defaultQueryRedirector;
  }
}

Fixed version (ground truth):
/** 
 * INTERNAL: Initialize the mappings as a separate step. This is done as a separate step to ensure that inheritance has been first resolved.
 */
public void initialize(AbstractSession session) throws DescriptorException {
  if (getHistoryPolicy() != null) {
    session.getProject().setHasGenericHistorySupport(true);
  }
  if (isIsolated()) {
    session.getProject().setHasIsolatedClasses(true);
  }
  if (!shouldIsolateObjectsInUnitOfWork() && !shouldBeReadOnly()) {
    session.getProject().setHasNonIsolatedUOWClasses(true);
  }
  if (isInitialized(INITIALIZED) || isInvalid()) {
    return;
  }
  setInitializationStage(INITIALIZED);
  if (isChildDescriptor()) {
    getInheritancePolicy().getParentDescriptor().initialize(session);
    if (getInheritancePolicy().getParentDescriptor().isIsolated()) {
      setIsIsolated(true);
    }
    getInheritancePolicy().initializeOptimisticLocking();
  }
  if (shouldOrderMappings()) {
    Vector mappings=getMappings();
    Object[] mappingsArray=new Object[mappings.size()];
    for (int index=0; index < mappings.size(); index++) {
      mappingsArray[index]=mappings.elementAt(index);
    }
    Arrays.sort(mappingsArray,new MappingCompare());
    mappings=NonSynchronizedVector.newInstance(mappingsArray.length);
    for (int index=0; index < mappingsArray.length; index++) {
      mappings.addElement(mappingsArray[index]);
    }
    setMappings(mappings);
  }
  for (Enumeration mappingsEnum=getMappings().elements(); mappingsEnum.hasMoreElements(); ) {
    DatabaseMapping mapping=(DatabaseMapping)mappingsEnum.nextElement();
    validateMappingType(mapping);
    mapping.initialize(session);
    if (mapping.isLockableMapping()) {
      getLockableMappings().add(mapping);
    }
    if ((mapping.isForeignReferenceMapping()) && (((ForeignReferenceMapping)mapping).getIndirectionPolicy() instanceof ProxyIndirectionPolicy)) {
      session.getProject().setHasProxyIndirection(true);
    }
    if ((usesOptimisticLocking() && getOptimisticLockingPolicy().isCascaded()) || hasCascadeLockingPolicies()) {
      prepareCascadeLockingPolicy(mapping);
    }
    if (mapping.derivesId()) {
      derivesIdMappings.put(mapping.getAttributeName(),mapping);
    }
    Helper.addAllUniqueToVector(getFields(),mapping.getFields());
  }
  if (hasMappingsPostCalculateChangesOnDeleted()) {
    session.getProject().setHasMappingsPostCalculateChangesOnDeleted(true);
  }
  if (!isAggregateDescriptor()) {
    if (!isChildDescriptor()) {
      if (usesOptimisticLocking()) {
        getOptimisticLockingPolicy().initializeProperties();
      }
    }
  }
  for (Iterator queryKeys=getQueryKeys().values().iterator(); queryKeys.hasNext(); ) {
    QueryKey queryKey=(QueryKey)queryKeys.next();
    queryKey.initialize(this);
  }
  if (hasInheritance()) {
    getInheritancePolicy().initialize(session);
    if (getInheritancePolicy().isChildDescriptor()) {
      for (Iterator iterator=getInheritancePolicy().getParentDescriptor().getMappings().iterator(); iterator.hasNext(); ) {
        DatabaseMapping mapping=(DatabaseMapping)iterator.next();
        if (mapping.isAggregateObjectMapping() || ((mapping.isForeignReferenceMapping() && (!mapping.isDirectCollectionMapping())) && (!((ForeignReferenceMapping)mapping).usesIndirection()))) {
          getLockableMappings().add(mapping);
        }
        if (mapping.derivesId()) {
          derivesIdMappings.put(mapping.getAttributeName(),mapping);
        }
      }
    }
  }
  if (hasInheritance() && shouldOrderMappings()) {
    Vector mappings=getMappings();
    Object[] mappingsArray=new Object[mappings.size()];
    for (int index=0; index < mappings.size(); index++) {
      mappingsArray[index]=mappings.elementAt(index);
    }
    Arrays.sort(mappingsArray,new MappingCompare());
    mappings=NonSynchronizedVector.newInstance(mappingsArray.length);
    for (int index=0; index < mappingsArray.length; index++) {
      mappings.addElement(mappingsArray[index]);
    }
    setMappings(mappings);
  }
  setAllFields((Vector)getFields().clone());
  getObjectBuilder().initialize(session);
  if (shouldOrderMappings()) {
    for (int index=getObjectBuilder().getPrimaryKeyMappings().size() - 1; index >= 0; index--) {
      DatabaseMapping mapping=getObjectBuilder().getPrimaryKeyMappings().get(index);
      if ((mapping != null) && mapping.isDirectToFieldMapping()) {
        getMappings().remove(mapping);
        getMappings().add(0,mapping);
        DatabaseField field=((AbstractDirectMapping)mapping).getField();
        getFields().remove(field);
        getFields().add(0,field);
        getAllFields().remove(field);
        getAllFields().add(0,field);
      }
    }
  }
  if (usesOptimisticLocking() && (!isChildDescriptor())) {
    getOptimisticLockingPolicy().initialize(session);
  }
  if (hasInterfacePolicy() || isDescriptorForInterface()) {
    interfaceInitialization(session);
  }
  if (hasWrapperPolicy()) {
    getWrapperPolicy().initialize(session);
  }
  if (hasReturningPolicy()) {
    getReturningPolicy().initialize(session);
  }
  getQueryManager().initialize(session);
  getEventManager().initialize(session);
  getCopyPolicy().initialize(session);
  getInstantiationPolicy().initialize(session);
  if (getHistoryPolicy() != null) {
    getHistoryPolicy().initialize(session);
  }
 else   if (hasInheritance()) {
    ClassDescriptor parentDescriptor=getInheritancePolicy().getParentDescriptor();
    if ((parentDescriptor != null) && (parentDescriptor.getHistoryPolicy() != null)) {
      setHistoryPolicy((HistoryPolicy)parentDescriptor.getHistoryPolicy().clone());
    }
  }
  if (this.getCMPPolicy() != null) {
    this.getCMPPolicy().initialize(this,session);
  }
  if (hasFetchGroupManager()) {
    getFetchGroupManager().initialize(session);
  }
  if ((getObjectChangePolicyInternal() == null) && (ChangeTracker.class.isAssignableFrom(getJavaClass()))) {
    if (Arrays.asList(getJavaClass().getInterfaces()).contains(PersistenceWeavedChangeTracking.class)) {
      if (supportsChangeTracking(session.getProject())) {
        setObjectChangePolicy(new AttributeChangeTrackingPolicy());
      }
    }
  }
  getObjectChangePolicy().initialize(session,this);
  if (getUnitOfWorkCacheIsolationLevel() == UNDEFINED_ISOLATATION) {
    if (isIsolated()) {
      setUnitOfWorkCacheIsolationLevel(ISOLATE_CACHE_ALWAYS);
    }
 else {
      setUnitOfWorkCacheIsolationLevel(ISOLATE_NEW_DATA_AFTER_TRANSACTION);
    }
  }
  if (getIdValidation() == null) {
    if (getPrimaryKeyFields().size() > 1) {
      setIdValidation(IdValidation.NULL);
    }
 else {
      setIdValidation(IdValidation.ZERO);
    }
  }
  if (this.defaultReadAllQueryRedirector == null) {
    this.defaultReadAllQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultReadObjectQueryRedirector == null) {
    this.defaultReadObjectQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultReportQueryRedirector == null) {
    this.defaultReportQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultInsertObjectQueryRedirector == null) {
    this.defaultInsertObjectQueryRedirector=this.defaultQueryRedirector;
  }
  if (this.defaultUpdateObjectQueryRedirector == null) {
    this.defaultUpdateObjectQueryRedirector=this.defaultQueryRedirector;
  }
}