public static OptimisticLockException noVersionNumberWhenDeleting(
      Object object, ObjectLevelModifyQuery query) {
    Vector key = new Vector();
    if (query.getSession() != null) {
      key = query.getSession().keyFromObject(object);
    }
    Object[] args = {object, object.getClass().getName(), key, CR};

    OptimisticLockException optimisticLockException =
        new OptimisticLockException(
            ExceptionMessageGenerator.buildMessage(
                OptimisticLockException.class, NO_VERSION_NUMBER_WHEN_DELETING, args),
            query);
    optimisticLockException.setErrorCode(NO_VERSION_NUMBER_WHEN_DELETING);
    return optimisticLockException;
  }
  public static OptimisticLockException objectChangedSinceLastReadWhenUpdating(
      Object object, ObjectLevelModifyQuery query) {
    Vector key = new Vector();
    if (query.getSession() != null) {
      key = query.getSession().keyFromObject(object);
    }
    Object[] args = {object, object.getClass().getName(), key, CR};

    OptimisticLockException optimisticLockException =
        new OptimisticLockException(
            ExceptionMessageGenerator.buildMessage(
                OptimisticLockException.class, OBJECT_CHANGED_SINCE_LAST_READ_WHEN_UPDATING, args),
            query);
    optimisticLockException.setErrorCode(OBJECT_CHANGED_SINCE_LAST_READ_WHEN_UPDATING);
    return optimisticLockException;
  }
  /**
   * Return whether any process leading to object modification should also affect its parts. Used by
   * write, insert, update, and delete.
   */
  protected boolean shouldObjectModifyCascadeToParts(ObjectLevelModifyQuery query) {
    if (isForeignKeyRelationship()) {
      return super.shouldObjectModifyCascadeToParts(query);
    } else {
      if (this.isReadOnly()) {
        return false;
      }

      if (this.isPrivateOwned()) {
        return true;
      }

      return query.shouldCascadeAllParts();
    }
  }
  /** INTERNAL: Get a value from the object and set that in the respective field of the row. */
  @Override
  public void writeFromObjectIntoRowForWhereClause(
      ObjectLevelModifyQuery query, AbstractRecord record) {
    if (isReadOnly()) {
      return;
    }
    Object object;
    if (query.isDeleteObjectQuery()) {
      object = query.getObject();
    } else {
      object = query.getBackupClone();
    }
    Object referenceObject = getRealAttributeValueFromObject(object, query.getSession());

    if (referenceObject == null) {
      writeFromNullObjectIntoRow(record);
    } else {
      if (isForeignKeyRelationship()) {
        Enumeration sourceFields = getForeignKeyFields().elements();
        ClassDescriptor descriptor = query.getSession().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, query.getSession());
          record.put(sourceKey, referenceValue);
        }
      }
      if (getTypeField() != null) {
        record.put(getTypeField(), getTypeForImplementor(referenceObject.getClass()));
      }
    }
  }
 /** INTERNAL: TopLink exceptions should only be thrown by TopLink. */
 protected OptimisticLockException(String theMessage, ObjectLevelModifyQuery query) {
   super(theMessage);
   this.query = query;
   query.getSession().incrementProfile(SessionProfiler.OptimisticLockException);
 }