Ejemplo n.º 1
0
  /** Delete all of the objects with the matching class. */
  public void deleteAllObjects(Class theClass, List objects, AbstractSession session) {
    ClassDescriptor descriptor = null;

    if (((UnitOfWorkImpl) session).shouldOrderUpdates()) { // bug 331064 - Sort the delete order
      objects = sort(theClass, objects);
    }

    int size = objects.size();
    for (int index = 0; index < size; index++) {
      Object objectToDelete = objects.get(index);
      if (objectToDelete.getClass() == theClass) {
        if (descriptor == null) {
          descriptor = session.getDescriptor(theClass);
        }
        // PERF: Get the descriptor query, to avoid extra query creation.
        DeleteObjectQuery deleteQuery = descriptor.getQueryManager().getDeleteQuery();
        if (deleteQuery == null) {
          deleteQuery = new DeleteObjectQuery();
          deleteQuery.setDescriptor(descriptor);
        } else {
          // Ensure original query has been prepared.
          deleteQuery.checkPrepare(session, deleteQuery.getTranslationRow());
          deleteQuery = (DeleteObjectQuery) deleteQuery.clone();
        }
        deleteQuery.setIsExecutionClone(true);
        deleteQuery.setObject(objectToDelete);
        session.executeQuery(deleteQuery);
      }
    }
  }
  /** INTERNAL: Get a value from the object and set that in the respective field of the row. */
  @Override
  public Object valueFromObject(Object object, DatabaseField field, AbstractSession session) {
    // First check if the value can be obtained from the value holder's row.
    AbstractRecord referenceRow =
        getIndirectionPolicy().extractReferenceRow(getAttributeValueFromObject(object));
    if (referenceRow != null) {
      Object value = referenceRow.get(field);

      // Must ensure the classification to get a cache hit.
      try {
        value = session.getDatasourcePlatform().convertObject(value, getFieldClassification(field));
      } catch (ConversionException e) {
        throw ConversionException.couldNotBeConverted(this, getDescriptor(), e);
      }

      return value;
    }

    // 2.5.1.6 PWK.  added to support batch reading on variable one to ones
    Object referenceObject = getRealAttributeValueFromObject(object, session);
    String queryKeyName = (String) getSourceToTargetQueryKeyNames().get(field);
    ClassDescriptor objectDescriptor = session.getDescriptor(referenceObject.getClass());
    DatabaseField targetField =
        objectDescriptor.getObjectBuilder().getTargetFieldForQueryKeyName(queryKeyName);

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

    return objectDescriptor
        .getObjectBuilder()
        .extractValueFromObjectForField(referenceObject, targetField, session);
  }
Ejemplo n.º 3
0
 /**
  * Commit all 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 commitNewObjectsForClassWithChangeSet(
     UnitOfWorkChangeSet uowChangeSet, Class theClass) {
   Map<ObjectChangeSet, ObjectChangeSet> newObjectChangesList =
       uowChangeSet.getNewObjectChangeSets().get(theClass);
   if (newObjectChangesList != null) { // may be no changes for that class type.
     AbstractSession session = getSession();
     ClassDescriptor descriptor = session.getDescriptor(theClass);
     List<ObjectChangeSet> newChangeSets = new ArrayList(newObjectChangesList.values());
     int size = newChangeSets.size();
     for (int index = 0; index < size; index++) {
       ObjectChangeSet changeSetToWrite = newChangeSets.get(index);
       Object objectToWrite = changeSetToWrite.getUnitOfWorkClone();
       if (!isProcessedCommit(objectToWrite)) {
         // PERF: Get the descriptor query, to avoid extra query creation.
         InsertObjectQuery commitQuery = descriptor.getQueryManager().getInsertQuery();
         if (commitQuery == null) {
           commitQuery = new InsertObjectQuery();
           commitQuery.setDescriptor(descriptor);
         } else {
           // Ensure original query has been prepared.
           commitQuery.checkPrepare(session, commitQuery.getTranslationRow());
           commitQuery = (InsertObjectQuery) commitQuery.clone();
         }
         commitQuery.setIsExecutionClone(true);
         commitQuery.setObjectChangeSet(changeSetToWrite);
         commitQuery.setObject(objectToWrite);
         commitQuery.cascadeOnlyDependentParts();
         commitQuery.setModifyRow(null);
         session.executeQuery(commitQuery);
       }
       uowChangeSet.putNewObjectInChangesList(changeSetToWrite, session);
     }
   }
 }
  /**
   * INTERNAL: Compare the references of the two objects are the same, not the objects themselves.
   * Used for independent relationships. This is used for testing and validation purposes.
   *
   * <p>Must get separate fields for the objects because we may be adding a different class to the
   * attribute because of the interface
   */
  @Override
  protected boolean compareObjectsWithoutPrivateOwned(
      Object firstObject, Object secondObject, AbstractSession session) {
    Object firstPrivateObject = getRealAttributeValueFromObject(firstObject, session);
    Object secondPrivateObject = getRealAttributeValueFromObject(secondObject, session);

    if ((firstPrivateObject == null) && (secondPrivateObject == null)) {
      return true;
    }

    if ((firstPrivateObject == null) || (secondPrivateObject == null)) {
      return false;
    }
    if (firstPrivateObject.getClass() != secondPrivateObject.getClass()) {
      return false;
    }
    Iterator targetKeys = getSourceToTargetQueryKeyNames().values().iterator();
    ClassDescriptor descriptor = session.getDescriptor(firstPrivateObject.getClass());
    ClassDescriptor descriptor2 = session.getDescriptor(secondPrivateObject.getClass());

    while (targetKeys.hasNext()) {
      String queryKey = (String) targetKeys.next();
      DatabaseField field = descriptor.getObjectBuilder().getFieldForQueryKeyName(queryKey);
      Object firstObjectField =
          descriptor
              .getObjectBuilder()
              .extractValueFromObjectForField(firstPrivateObject, field, session);
      DatabaseField field2 = descriptor2.getObjectBuilder().getFieldForQueryKeyName(queryKey);
      Object secondObjectField =
          descriptor2
              .getObjectBuilder()
              .extractValueFromObjectForField(secondPrivateObject, field2, session);

      if (!((firstObjectField == null) && (secondObjectField == null))) {
        if ((firstObjectField == null) || (secondObjectField == null)) {
          return false;
        }
        if (!firstObjectField.equals(secondObjectField)) {
          return false;
        }
      }
    }

    return true;
  }
Ejemplo n.º 5
0
 // bug 331064 - Sort the delete order based on PKs.
 private List sort(Class theClass, List objects) {
   ClassDescriptor descriptor = session.getDescriptor(theClass);
   org.eclipse.persistence.internal.descriptors.ObjectBuilder objectBuilder =
       descriptor.getObjectBuilder();
   int size = objects.size();
   TreeMap sortedObjects = new TreeMap();
   for (int index = 0; index < size; index++) {
     Object objectToDelete = objects.get(index);
     if (objectToDelete.getClass() == theClass) {
       sortedObjects.put(
           objectBuilder.extractPrimaryKeyFromObject(objectToDelete, session), objectToDelete);
     }
   }
   return new ArrayList(sortedObjects.values());
 }
  /** 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);
  }
Ejemplo n.º 7
0
 /**
  * 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: Get a value from the object and set that in the respective field of the row. If the
   * mapping id target foreign key, you must only write the type into the roe, the rest will be
   * updated when the object itself is written
   */
  @Override
  public void writeFromObjectIntoRowWithChangeRecord(
      ChangeRecord changeRecord, AbstractRecord record, AbstractSession session) {
    if (isReadOnly()) {
      return;
    }

    ObjectChangeSet changeSet =
        (ObjectChangeSet) ((ObjectReferenceChangeRecord) changeRecord).getNewValue();
    if (changeSet == null) {
      writeFromNullObjectIntoRow(record);
    } else {
      Object referenceObject = changeSet.getUnitOfWorkClone();
      if (isForeignKeyRelationship()) {
        Enumeration sourceFields = getForeignKeyFields().elements();
        ClassDescriptor descriptor = session.getDescriptor(referenceObject.getClass());
        while (sourceFields.hasMoreElements()) {
          DatabaseField sourceKey = (DatabaseField) sourceFields.nextElement();
          String targetQueryKey = (String) getSourceToTargetQueryKeyNames().get(sourceKey);
          DatabaseField targetKeyField =
              descriptor.getObjectBuilder().getFieldForQueryKeyName(targetQueryKey);
          if (targetKeyField == null) {
            throw DescriptorException.variableOneToOneMappingIsNotDefinedProperly(
                this, descriptor, targetQueryKey);
          }
          Object referenceValue =
              descriptor
                  .getObjectBuilder()
                  .extractValueFromObjectForField(referenceObject, targetKeyField, session);
          record.put(sourceKey, referenceValue);
        }
      }
      if (getTypeField() != null) {
        record.put(getTypeField(), getTypeForImplementor(referenceObject.getClass()));
      }
    }
  }
 /** INTERNAL: Build and return a change set for the specified element. */
 public Object buildChangeSet(Object element, ObjectChangeSet owner, AbstractSession session) {
   ObjectBuilder objectBuilder = session.getDescriptor(element).getObjectBuilder();
   return objectBuilder.createObjectChangeSet(
       element, (UnitOfWorkChangeSet) owner.getUOWChangeSet(), session);
 }
 /**
  * INTERNAL: Return a descriptor for the target of this mapping For normal
  * ObjectReferenceMappings, we return the reference descriptor. For a VariableOneToOneMapping, the
  * reference descriptor is often a descriptor for an interface and does not contain adequate
  * information. As a result, we look up the descriptor for the specific class we are looking for
  * Bug 2612571
  */
 @Override
 public ClassDescriptor getDescriptorForTarget(Object targetObject, AbstractSession session) {
   return session.getDescriptor(targetObject);
 }