Search in sources :

Example 1 with ObjectBuilder

use of org.eclipse.persistence.internal.descriptors.ObjectBuilder in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method registerNewContainerBean.

/**
 * INTERNAL:
 * Register the new container bean with the unit of work.
 * Normally the registerObject method should be used for all registration of new and existing objects.
 * This version of the register method can only be used for new container beans.
 *
 * @see #registerObject(Object)
 */
public Object registerNewContainerBean(Object newObject) {
    if (newObject == null) {
        return null;
    }
    // CR#2272
    logDebugMessage(newObject, "register_new");
    startOperationProfile(SessionProfiler.Register);
    setShouldNewObjectsBeCached(true);
    ClassDescriptor descriptor = getDescriptor(newObject);
    if (descriptor == null) {
        throw DescriptorException.missingDescriptor(newObject.getClass().toString());
    }
    ObjectBuilder builder = descriptor.getObjectBuilder();
    // Ensure that the registered object is the one from the parent cache.
    if (shouldPerformFullValidation()) {
        Object primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(newObject, this);
        Object objectFromCache = this.parent.getIdentityMapAccessorInstance().getFromIdentityMap(primaryKey, descriptor.getJavaClass(), descriptor);
        if (objectFromCache != null) {
            throw ValidationException.wrongObjectRegistered(newObject, objectFromCache);
        }
    }
    Object original = builder.buildNewInstance();
    builder.copyInto(newObject, original);
    Object clone = registerObject(original);
    getContainerBeans().put(newObject, clone);
    endOperationProfile(SessionProfiler.Register);
    return newObject;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder)

Example 2 with ObjectBuilder

use of org.eclipse.persistence.internal.descriptors.ObjectBuilder in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method getObjectFromNewObjects.

/**
 * INTERNAL:
 * Return any new object matching the expression.
 * Used for in-memory querying.
 */
public Object getObjectFromNewObjects(Class<?> theClass, Object selectionKey) {
    // PERF: Avoid initialization of new objects if none.
    if (!hasNewObjects()) {
        return null;
    }
    // bug 327900 - If don't read subclasses is set on the descriptor heed it.
    ClassDescriptor descriptor = getDescriptor(theClass);
    boolean readSubclassesOrNoInheritance = (!descriptor.hasInheritance() || descriptor.getInheritancePolicy().shouldReadSubclasses());
    ObjectBuilder objectBuilder = descriptor.getObjectBuilder();
    for (Iterator newObjectsEnum = getNewObjectsCloneToOriginal().keySet().iterator(); newObjectsEnum.hasNext(); ) {
        Object object = newObjectsEnum.next();
        // bug 327900
        if ((object.getClass() == theClass) || (readSubclassesOrNoInheritance && (theClass.isInstance(object)))) {
            Object primaryKey = objectBuilder.extractPrimaryKeyFromObject(object, this, true);
            if ((primaryKey != null) && primaryKey.equals(selectionKey)) {
                return object;
            }
        }
    }
    return null;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Iterator(java.util.Iterator) DescriptorIterator(org.eclipse.persistence.internal.descriptors.DescriptorIterator) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder)

Example 3 with ObjectBuilder

use of org.eclipse.persistence.internal.descriptors.ObjectBuilder in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method registerExistingObject.

/**
 * ADVANCED:
 * Register the existing object with the unit of work.
 * This is a advanced API that can be used if the application can guarantee the object exists on the database.
 * When registerObject is called the unit of work determines existence through the descriptor's doesExist setting.
 *
 * @return The clone of the original object, the return value must be used for editing.
 * Editing the original is not allowed in the unit of work.
 */
public Object registerExistingObject(Object existingObject, boolean isFromSharedCache) {
    if (existingObject == null) {
        return null;
    }
    ClassDescriptor descriptor = getDescriptor(existingObject);
    if (descriptor == null) {
        throw DescriptorException.missingDescriptor(existingObject.getClass().toString());
    }
    if (this.isClassReadOnly(descriptor.getJavaClass(), descriptor)) {
        return existingObject;
    }
    ObjectBuilder builder = descriptor.getObjectBuilder();
    Object implementation = builder.unwrapObject(existingObject, this);
    Object registeredObject = this.registerExistingObject(implementation, descriptor, null, isFromSharedCache);
    // Bug # 3212057 - workaround JVM bug (MWN)
    if (implementation != existingObject) {
        return builder.wrapObject(registeredObject, this);
    } else {
        return registeredObject;
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder)

Example 4 with ObjectBuilder

use of org.eclipse.persistence.internal.descriptors.ObjectBuilder in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method buildOriginal.

/**
 * INTERNAL:
 * Unregistered new objects have no original so we must create one for commit and resume and
 * to put into the parent.  We can NEVER let the same copy of an object exist in multiple units of work.
 */
public Object buildOriginal(Object workingClone) {
    ClassDescriptor descriptor = getDescriptor(workingClone);
    ObjectBuilder builder = descriptor.getObjectBuilder();
    Object original = builder.instantiateClone(workingClone, this);
    // -An unregistered new object
    if (checkIfAlreadyRegistered(workingClone, descriptor) != null) {
        getCloneToOriginals().put(workingClone, original);
        return original;
    } else {
        // Assume it is an unregisteredNewObject, but this is worrisome, as
        // it may be an unregistered existing object, not in the parent cache?
        Object backup = builder.instantiateClone(workingClone, this);
        // Original is fine for backup as state is the same.
        getCloneMapping().put(workingClone, backup);
        // Must register new instance / clone as the original.
        getNewObjectsCloneToOriginal().put(workingClone, original);
        getNewObjectsOriginalToClone().put(original, workingClone);
    // no need to register in identity map as the DatabaseQueryMechanism will have
    // placed the object in the identity map on insert.  bug 3431586
    }
    return original;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder)

Example 5 with ObjectBuilder

use of org.eclipse.persistence.internal.descriptors.ObjectBuilder in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method registerNewObject.

/**
 * ADVANCED:
 * Register the new object with the unit of work.
 * This will register the new object without cloning.
 * Normally the registerObject method should be used for all registration of new and existing objects.
 * This version of the register method can only be used for new objects.
 * This method should only be used if a new object is desired to be registered without cloning.
 *
 * @see #registerObject(Object)
 */
@Override
public Object registerNewObject(Object newObject) {
    if (newObject == null) {
        return null;
    }
    ClassDescriptor descriptor = getDescriptor(newObject);
    if (descriptor == null) {
        throw DescriptorException.missingDescriptor(newObject.getClass().toString());
    }
    ObjectBuilder builder = descriptor.getObjectBuilder();
    Object implementation = builder.unwrapObject(newObject, this);
    this.registerNewObject(implementation, descriptor);
    if (implementation == newObject) {
        return newObject;
    } else {
        return builder.wrapObject(implementation, this);
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder)

Aggregations

ObjectBuilder (org.eclipse.persistence.internal.descriptors.ObjectBuilder)75 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)19 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)13 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)8 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)7 ArrayList (java.util.ArrayList)6 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)6 HashMap (java.util.HashMap)5 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)5 Map (java.util.Map)4 CacheKey (org.eclipse.persistence.internal.identitymaps.CacheKey)4 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 List (java.util.List)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 Vector (java.util.Vector)2 AttributeChangeTrackingPolicy (org.eclipse.persistence.descriptors.changetracking.AttributeChangeTrackingPolicy)2 Expression (org.eclipse.persistence.expressions.Expression)2 PersistenceEntity (org.eclipse.persistence.internal.descriptors.PersistenceEntity)2