/**
  * INTERNAL: Execute the query building the objects directly from the database result-set.
  *
  * @exception DatabaseException - an error has occurred on the database
  * @return object - the first object found or null if none.
  */
 protected Object executeObjectLevelReadQueryFromResultSet() throws DatabaseException {
   UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) getSession();
   DatabaseAccessor accessor = (DatabaseAccessor) unitOfWork.getAccessor();
   DatabasePlatform platform = accessor.getPlatform();
   DatabaseCall call = (DatabaseCall) getCall().clone();
   call.setQuery(this);
   call.translate(this.translationRow, null, unitOfWork);
   Statement statement = null;
   ResultSet resultSet = null;
   boolean exceptionOccured = false;
   try {
     accessor.incrementCallCount(unitOfWork);
     statement = call.prepareStatement(accessor, this.translationRow, unitOfWork);
     resultSet = accessor.executeSelect(call, statement, unitOfWork);
     ResultSetMetaData metaData = resultSet.getMetaData();
     Vector results = new Vector();
     ObjectBuilder builder = this.descriptor.getObjectBuilder();
     while (resultSet.next()) {
       results.add(
           builder.buildWorkingCopyCloneFromResultSet(
               this,
               this.joinedAttributeManager,
               resultSet,
               unitOfWork,
               accessor,
               metaData,
               platform));
     }
     return results;
   } catch (SQLException exception) {
     exceptionOccured = true;
     DatabaseException commException =
         accessor.processExceptionForCommError(session, exception, call);
     if (commException != null) throw commException;
     throw DatabaseException.sqlException(exception, call, accessor, unitOfWork, false);
   } finally {
     try {
       if (resultSet != null) {
         resultSet.close();
       }
       if (statement != null) {
         accessor.releaseStatement(statement, call.getSQLString(), call, unitOfWork);
       }
     } catch (SQLException exception) {
       if (!exceptionOccured) {
         // in the case of an external connection pool the connection may be null after the
         // statement release
         // if it is null we will be unable to check the connection for a comm error and
         // therefore must return as if it was not a comm error.
         DatabaseException commException =
             accessor.processExceptionForCommError(session, exception, call);
         if (commException != null) throw commException;
         throw DatabaseException.sqlException(exception, call, accessor, session, false);
       }
     }
   }
 }
 /**
  * INTERNAL: Retrieve the target object's primary key value that is mapped to a given source xpath
  * (in the source-target key field association list).
  *
  * @param targetObject - the reference class instance that holds the required pk value
  * @param xmlFld
  * @param session
  * @return null if the target object is null, the reference class is null, or a primary key field
  *     name does not exist on the reference descriptor that matches the target field name -
  *     otherwise, return the associated primary key value
  */
 public Object buildFieldValue(Object targetObject, XMLField xmlFld, AbstractSession session) {
   if (targetObject == null || getReferenceClass() == null) {
     return null;
   }
   ClassDescriptor descriptor = getReferenceDescriptor();
   ObjectBuilder objectBuilder = descriptor.getObjectBuilder();
   Vector pks = objectBuilder.extractPrimaryKeyFromObject(targetObject, session);
   XMLField tgtXMLField = (XMLField) getSourceToTargetKeyFieldAssociations().get(xmlFld);
   int idx = descriptor.getPrimaryKeyFields().indexOf(tgtXMLField);
   if (idx == -1) {
     return null;
   }
   return pks.get(idx);
 }
 // 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());
 }
Esempio n. 4
0
  /** INTERNAL: Create an instance of the Id class or value from the object. */
  public Object createPrimaryKeyInstance(Object object, AbstractSession session) {
    KeyElementAccessor[] pkElementArray = this.getKeyClassFields();
    ObjectBuilder builder = getDescriptor().getObjectBuilder();
    if (pkElementArray.length == 1 && pkElementArray[0] instanceof KeyIsElementAccessor) {
      DatabaseMapping mapping =
          builder.getMappingForAttributeName(pkElementArray[0].getAttributeName());
      Object fieldValue = mapping.getRealAttributeValueFromObject(object, session);
      if (mapping.isObjectReferenceMapping()) {
        fieldValue =
            mapping
                .getReferenceDescriptor()
                .getCMPPolicy()
                .createPrimaryKeyInstance(fieldValue, session);
      }
      return fieldValue;
    }

    Object keyInstance = getPKClassInstance();
    Set<ObjectReferenceMapping> usedObjectReferenceMappings = new HashSet<ObjectReferenceMapping>();
    for (int index = 0; index < pkElementArray.length; index++) {
      Object keyObj = object;
      KeyElementAccessor accessor = pkElementArray[index];
      DatabaseField field = accessor.getDatabaseField();
      DatabaseMapping mapping = builder.getMappingForField(field);
      // With session validation, the mapping shouldn't be null at this
      // point, don't bother checking.
      if (!mapping.isObjectReferenceMapping() || !usedObjectReferenceMappings.contains(mapping)) {
        while (mapping.isAggregateObjectMapping()) {
          keyObj = mapping.getRealAttributeValueFromObject(keyObj, session);
          mapping = mapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(field);
        }
        Object fieldValue = mapping.getRealAttributeValueFromObject(keyObj, session);
        if (mapping.isObjectReferenceMapping()) {
          fieldValue =
              mapping
                  .getReferenceDescriptor()
                  .getCMPPolicy()
                  .createPrimaryKeyInstance(fieldValue, session);
          usedObjectReferenceMappings.add((ObjectReferenceMapping) mapping);
        }
        accessor.setValue(keyInstance, fieldValue);
      }
    }

    return keyInstance;
  }
 /** 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);
 }