Search in sources :

Example 21 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class AdapterWithReturnObjectControl method getObjectForInsert.

@Override
public Object getObjectForInsert(Session session, Object objectToInsert) {
    ClassDescriptor desc = session.getClassDescriptor(objectToInsert);
    DataRecord rowToInsert = desc.getObjectBuilder().buildRow(objectToInsert, (AbstractSession) session, WriteType.INSERT);
    DataRecord rowReturn = getRowForInsert(rowToInsert);
    if (rowReturn != null && !rowReturn.isEmpty()) {
        DataRecord row = new DatabaseRecord(rowToInsert.size());
        row.putAll(rowToInsert);
        row.putAll(rowReturn);
        return readObjectFromRow(session, desc, row);
    } else {
        return objectToInsert;
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord)

Example 22 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class TargetInvocationWhileInstantiatingMethodBasedProxyTest method setup.

@Override
protected void setup() throws NoSuchMethodException {
    descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(TargetInvocationWhileInstantiatingMethodBasedProxyTest.class);
    descriptor.addTableName("Dummy_Table");
    row = new DatabaseRecord();
    Class<?>[] parmClasses = { DatabaseRecord.class };
    theMethod = TargetInvocationWhileInstantiatingMethodBasedProxyTest.class.getDeclaredMethod("invalidMethod", parmClasses);
    theTransformer = new MethodBasedAttributeTransformer();
    theTransformer.setAttributeTransformationMethod(theMethod);
    theReceiver = new TargetInvocationWhileInstantiatingMethodBasedProxyTest();
    valueHolder = new TransformerBasedValueHolder(theTransformer, theReceiver, row, (AbstractSession) getSession());
    expectedException = DescriptorException.targetInvocationWhileInstantiatingMethodBasedProxy(new Exception());
}
Also used : TransformerBasedValueHolder(org.eclipse.persistence.internal.indirection.TransformerBasedValueHolder) RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) MethodBasedAttributeTransformer(org.eclipse.persistence.mappings.transformers.MethodBasedAttributeTransformer) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 23 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class TargetInvocationWhileInvokingAttributeMethodTest method setup.

@Override
protected void setup() {
    descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(TargetInvocationWhileInvokingAttributeMethodTest.class);
    descriptor.addTableName("EMPLOYEE");
    mapping = new TransformationMapping();
    mapping.setAttributeName("normalHours");
    mapping.setAttributeTransformation("invalidMethod");
    descriptor.addMapping(mapping);
    mapping.initialize((AbstractSession) getSession());
    row = new DatabaseRecord();
    expectedException = DescriptorException.targetInvocationWhileInvokingAttributeMethod(mapping, new Exception());
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) TransformationMapping(org.eclipse.persistence.mappings.TransformationMapping) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException)

Example 24 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class TargetInvocationWhileInvokingRowExtractionMethodTest method setup.

@Override
protected void setup() throws NoSuchMethodException {
    descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(TargetInvocationWhileInvokingRowExtractionMethodTest.class);
    descriptor.addTableName("PROJECT");
    descriptor.getInheritancePolicy().setParentClass(null);
    descriptor.getInheritancePolicy().dontReadSubclassesOnQueries();
    descriptor.getInheritancePolicy().setClassIndicatorFieldName("PROJECT.PROJ_TYPE");
    descriptor.getInheritancePolicy().setShouldUseClassNameAsIndicator(true);
    policy = descriptor.getInheritancePolicy();
    policy.setClassExtractionMethodName("invalidMethod");
    policy.preInitialize((AbstractSession) getSession());
    row = new DatabaseRecord();
    Class<?>[] parmClasses = { DataRecord.class };
    expectedException = DescriptorException.targetInvocationWhileInvokingRowExtractionMethod(row, TargetInvocationWhileInvokingRowExtractionMethodTest.class.getDeclaredMethod("invalidMethod", parmClasses), descriptor, new Exception());
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException)

Example 25 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class AggregateObjectMapping method readFromReturnRowIntoObject.

/**
 * INTERNAL:
 * Build an aggregate object from the specified return row and put it
 * in the specified target object.
 * Return row is merged into object after execution of insert or update call
 * according to ReturningPolicy.
 * If not null changeSet must correspond to targetObject. changeSet is updated with all of the field values in the row.
 */
public Object readFromReturnRowIntoObject(AbstractRecord row, Object targetObject, ReadObjectQuery query, Collection handledMappings, ObjectChangeSet changeSet) throws DatabaseException {
    Object aggregate = getAttributeValueFromObject(targetObject);
    ObjectChangeSet aggregateChangeSet = null;
    if (aggregate == null) {
        aggregate = readFromRowIntoObject(row, null, targetObject, null, query, query.getSession(), true);
    } else {
        if (changeSet != null && (!changeSet.isNew() || (query.getDescriptor() != null && query.getDescriptor().shouldUseFullChangeSetsForNewObjects()))) {
            aggregateChangeSet = getReferenceDescriptor(aggregate, query.getSession()).getObjectBuilder().createObjectChangeSet(aggregate, (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet(), true, query.getSession());
        }
        AbstractRecord aggregateRow = new DatabaseRecord();
        int size = row.size();
        List<DatabaseField> fields = row.getFields();
        List values = row.getValues();
        List<DatabaseField> aggregateFields = getReferenceFields();
        for (int i = 0; i < size; i++) {
            DatabaseField field = fields.get(i);
            if (aggregateFields.contains(field)) {
                aggregateRow.add(field, values.get(i));
            }
        }
        getObjectBuilder(aggregate, query.getSession()).assignReturnRow(aggregate, query.getSession(), aggregateRow, aggregateChangeSet);
    }
    if (aggregate != null && isNullAllowed()) {
        boolean allAttributesNull = true;
        int nAggregateFields = this.fields.size();
        for (int i = 0; (i < nAggregateFields) && allAttributesNull; i++) {
            DatabaseField field = this.fields.elementAt(i);
            if (row.containsKey(field)) {
                allAttributesNull = row.get(field) == null;
            } else {
                Object fieldValue = valueFromObject(targetObject, field, query.getSession());
                if (fieldValue == null) {
                    Object baseValue = getDescriptor().getObjectBuilder().getBaseValueForField(field, targetObject);
                    if (baseValue != null) {
                        DatabaseMapping baseMapping = getDescriptor().getObjectBuilder().getBaseMappingForField(field);
                        if (baseMapping.isForeignReferenceMapping()) {
                            ForeignReferenceMapping refMapping = (ForeignReferenceMapping) baseMapping;
                            if (refMapping.usesIndirection()) {
                                allAttributesNull = refMapping.getIndirectionPolicy().objectIsInstantiated(baseValue);
                            }
                        } else if (baseMapping.isTransformationMapping()) {
                            AbstractTransformationMapping transMapping = (AbstractTransformationMapping) baseMapping;
                            if (transMapping.usesIndirection()) {
                                allAttributesNull = transMapping.getIndirectionPolicy().objectIsInstantiated(baseValue);
                            }
                        }
                    }
                } else {
                    allAttributesNull = false;
                }
            }
        }
        if (allAttributesNull) {
            aggregate = null;
            setAttributeValueInObject(targetObject, aggregate);
        }
    }
    if (changeSet != null && (!changeSet.isNew() || (query.getDescriptor() != null && query.getDescriptor().shouldUseFullChangeSetsForNewObjects()))) {
        AggregateChangeRecord record = (AggregateChangeRecord) changeSet.getChangesForAttributeNamed(getAttributeName());
        if (aggregate == null) {
            if (record != null) {
                record.setChangedObject(null);
            }
        } else {
            if (record == null) {
                record = new AggregateChangeRecord(changeSet);
                record.setAttribute(getAttributeName());
                record.setMapping(this);
                changeSet.addChange(record);
            }
            if (aggregateChangeSet == null) {
                // the old aggregate value was null
                aggregateChangeSet = getReferenceDescriptor(aggregate, query.getSession()).getObjectBuilder().createObjectChangeSet(aggregate, (UnitOfWorkChangeSet) ((UnitOfWorkImpl) query.getSession()).getUnitOfWorkChangeSet(), true, query.getSession());
            }
            record.setChangedObject(aggregateChangeSet);
        }
    }
    if (handledMappings != null) {
        handledMappings.add(this);
    }
    return aggregate;
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) UnitOfWorkChangeSet(org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) AbstractTransformationMapping(org.eclipse.persistence.mappings.foundation.AbstractTransformationMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) List(java.util.List) ArrayList(java.util.ArrayList) AggregateChangeRecord(org.eclipse.persistence.internal.sessions.AggregateChangeRecord)

Aggregations

DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)110 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)44 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)43 Vector (java.util.Vector)31 ArrayList (java.util.ArrayList)17 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)17 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)14 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)14 Test (org.junit.Test)14 Expression (org.eclipse.persistence.expressions.Expression)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 HashMap (java.util.HashMap)11 Map (java.util.Map)10 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)7 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)7 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)6 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)6 SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)6