Search in sources :

Example 1 with ObjectReferenceMapping

use of org.eclipse.persistence.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class ManyToOneAccessor method process.

/**
 * INTERNAL:
 * Process a many to one setting into an EclipseLink OneToOneMapping.
 */
@Override
public void process() {
    super.process();
    // Initialize our mapping now with what we found.
    ObjectReferenceMapping mapping = initManyToOneMapping();
    if (mapping instanceof ManyToOneMapping) {
        // Process the owning keys for this mapping.
        processOwningMappingKeys((ManyToOneMapping) mapping);
    } else {
        processForeignKeyRelationship(mapping);
    }
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.mappings.ObjectReferenceMapping) ManyToOneMapping(org.eclipse.persistence.mappings.ManyToOneMapping)

Example 2 with ObjectReferenceMapping

use of org.eclipse.persistence.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class ObjectAccessor method initManyToOneMapping.

/**
 * INTERNAL:
 * Initialize a ManyToOneMapping.
 */
protected ObjectReferenceMapping initManyToOneMapping() {
    // Allow for different descriptor types (EIS) to create different mapping types.
    ObjectReferenceMapping mapping = getDescriptor().getClassDescriptor().newManyToOneMapping();
    processRelationshipMapping(mapping);
    mapping.setIsOptional(isOptional());
    mapping.setDerivesId(derivesId());
    // Process the indirection.
    processIndirection(mapping);
    // Process a @ReturnInsert and @ReturnUpdate (to log a warning message)
    processReturnInsertAndUpdate();
    return mapping;
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.mappings.ObjectReferenceMapping)

Example 3 with ObjectReferenceMapping

use of org.eclipse.persistence.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method loadJoinedAttributes.

protected void loadJoinedAttributes(ClassDescriptor concreteDescriptor, Object sourceObject, CacheKey cacheKey, AbstractRecord databaseRow, JoinedAttributeManager joinManager, ObjectBuildingQuery query, boolean isTargetProtected) {
    boolean useOnlyMappingsExcludedFromSOP = false;
    if (concreteDescriptor.hasSerializedObjectPolicy() && query.shouldUseSerializedObjectPolicy()) {
        // sopObject has not been deserialized, sourceObject must be cached
        useOnlyMappingsExcludedFromSOP = databaseRow.get(concreteDescriptor.getSerializedObjectPolicy().getField()) != null;
    }
    Boolean isUntriggeredResultSetRecord = null;
    List<Expression> joinExpressions = joinManager.getJoinedAttributeExpressions();
    int size = joinExpressions.size();
    for (int index = 0; index < size; index++) {
        QueryKeyExpression queryKeyExpression = (QueryKeyExpression) joinExpressions.get(index);
        QueryKeyExpression baseExpression = (QueryKeyExpression) joinManager.getJoinedAttributes().get(index);
        DatabaseMapping mapping = joinManager.getJoinedAttributeMappings().get(index);
        // Only worry about immediate (excluding aggregates) foreign reference mapping attributes.
        if (queryKeyExpression == baseExpression) {
            if (mapping == null) {
                throw ValidationException.missingMappingForAttribute(concreteDescriptor, queryKeyExpression.getName(), toString());
            } else {
                if (!useOnlyMappingsExcludedFromSOP || mapping.isOutSopObject()) {
                    // get the intermediate objects between this expression node and the base builder
                    Object intermediateValue = joinManager.getValueFromObjectForExpression(query.getExecutionSession(), sourceObject, (ObjectExpression) baseExpression.getBaseExpression());
                    // Bug 4230655 - do not replace instantiated valueholders.
                    Object attributeValue = mapping.getAttributeValueFromObject(intermediateValue);
                    if ((attributeValue != null) && mapping.isForeignReferenceMapping() && ((ForeignReferenceMapping) mapping).usesIndirection() && (!((ForeignReferenceMapping) mapping).getIndirectionPolicy().objectIsInstantiated(attributeValue))) {
                        if (mapping.isObjectReferenceMapping() && ((ObjectReferenceMapping) mapping).isForeignKeyRelationship() && !mapping.isPrimaryKeyMapping()) {
                            if (isUntriggeredResultSetRecord == null) {
                                isUntriggeredResultSetRecord = databaseRow instanceof ResultSetRecord && ((ResultSetRecord) databaseRow).hasResultSet();
                            }
                            if (isUntriggeredResultSetRecord) {
                                for (DatabaseField field : mapping.getFields()) {
                                    // extract the values from ResultSet into the row
                                    databaseRow.get(field);
                                }
                            }
                        }
                        AbstractSession session = query.getExecutionSession();
                        mapping.readFromRowIntoObject(databaseRow, joinManager, intermediateValue, cacheKey, query, query.getExecutionSession(), isTargetProtected);
                        session.getIdentityMapAccessorInstance().getIdentityMap(concreteDescriptor).lazyRelationshipLoaded(intermediateValue, (ValueHolderInterface) ((ForeignReferenceMapping) mapping).getIndirectionPolicy().getOriginalValueHolder(attributeValue, session), (ForeignReferenceMapping) mapping);
                    }
                }
            }
        }
    }
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.mappings.ObjectReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) ResultSetRecord(org.eclipse.persistence.internal.sessions.ResultSetRecord) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 4 with ObjectReferenceMapping

use of org.eclipse.persistence.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class ObjectBuilder method loadBatchReadAttributes.

protected void loadBatchReadAttributes(ClassDescriptor concreteDescriptor, Object sourceObject, CacheKey cacheKey, AbstractRecord databaseRow, ObjectBuildingQuery query, JoinedAttributeManager joinManager, boolean isTargetProtected) {
    boolean useOnlyMappingsExcludedFromSOP = false;
    if (concreteDescriptor.hasSerializedObjectPolicy() && query.shouldUseSerializedObjectPolicy()) {
        // if true then sopObject has not been deserialized, that means sourceObject has been cached.
        useOnlyMappingsExcludedFromSOP = databaseRow.get(concreteDescriptor.getSerializedObjectPolicy().getField()) != null;
    }
    boolean isUntriggeredResultSetRecord = databaseRow instanceof ResultSetRecord && ((ResultSetRecord) databaseRow).hasResultSet();
    List<Expression> batchExpressions = ((ReadAllQuery) query).getBatchReadAttributeExpressions();
    int size = batchExpressions.size();
    for (int index = 0; index < size; index++) {
        QueryKeyExpression queryKeyExpression = (QueryKeyExpression) batchExpressions.get(index);
        // Only worry about immediate attributes.
        if (queryKeyExpression.getBaseExpression().isExpressionBuilder()) {
            DatabaseMapping mapping = getMappingForAttributeName(queryKeyExpression.getName());
            if (mapping == null) {
                throw ValidationException.missingMappingForAttribute(concreteDescriptor, queryKeyExpression.getName(), this.toString());
            } else {
                if (!useOnlyMappingsExcludedFromSOP || mapping.isOutSopObject()) {
                    // Bug 4230655 - do not replace instantiated valueholders.
                    Object attributeValue = mapping.getAttributeValueFromObject(sourceObject);
                    if ((attributeValue != null) && mapping.isForeignReferenceMapping() && ((ForeignReferenceMapping) mapping).usesIndirection() && (!((ForeignReferenceMapping) mapping).getIndirectionPolicy().objectIsInstantiated(attributeValue))) {
                        if (isUntriggeredResultSetRecord && mapping.isObjectReferenceMapping() && ((ObjectReferenceMapping) mapping).isForeignKeyRelationship() && !mapping.isPrimaryKeyMapping()) {
                            // still need to extract values from ResultSet for foreign key fields.
                            for (DatabaseField field : mapping.getFields()) {
                                // extract the values from ResultSet into the row
                                databaseRow.get(field);
                            }
                        }
                        AbstractSession session = query.getExecutionSession();
                        mapping.readFromRowIntoObject(databaseRow, joinManager, sourceObject, cacheKey, query, query.getExecutionSession(), isTargetProtected);
                        session.getIdentityMapAccessorInstance().getIdentityMap(concreteDescriptor).lazyRelationshipLoaded(sourceObject, (ValueHolderInterface) ((ForeignReferenceMapping) mapping).getIndirectionPolicy().getOriginalValueHolder(attributeValue, session), (ForeignReferenceMapping) mapping);
                    }
                }
            }
        }
    }
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.mappings.ObjectReferenceMapping) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) ResultSetRecord(org.eclipse.persistence.internal.sessions.ResultSetRecord) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 5 with ObjectReferenceMapping

use of org.eclipse.persistence.mappings.ObjectReferenceMapping in project eclipselink by eclipse-ee4j.

the class CMPPolicy method createPrimaryKeyInstance.

/**
 * 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<>();
    for (int index = 0; index < pkElementArray.length; index++) {
        Object keyObj = object;
        KeyElementAccessor accessor = pkElementArray[index];
        DatabaseField field = accessor.getDatabaseField();
        DatabaseMapping mapping = builder.getMappingForField(field);
        Object nestedKeyInstance = keyInstance;
        // 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);
                // Check for embedded Id values
                if (mapping.isAggregateMapping()) {
                    Object nestedObject = mapping.getRealAttributeValueFromObject(nestedKeyInstance, session);
                    if (nestedObject == null) {
                        nestedObject = getClassInstance(mapping.getReferenceDescriptor().getJavaClass());
                    }
                    mapping.setRealAttributeValueInObject(nestedKeyInstance, nestedObject);
                    nestedKeyInstance = nestedObject;
                }
            }
            Object fieldValue = mapping.getRealAttributeValueFromObject(keyObj, session);
            if (mapping.isObjectReferenceMapping()) {
                fieldValue = mapping.getReferenceDescriptor().getCMPPolicy().createPrimaryKeyInstance(fieldValue, session);
                usedObjectReferenceMappings.add((ObjectReferenceMapping) mapping);
            }
            accessor.setValue(nestedKeyInstance, fieldValue);
        }
    }
    return keyInstance;
}
Also used : ObjectReferenceMapping(org.eclipse.persistence.mappings.ObjectReferenceMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder) HashSet(java.util.HashSet)

Aggregations

ObjectReferenceMapping (org.eclipse.persistence.mappings.ObjectReferenceMapping)9 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)5 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)5 Expression (org.eclipse.persistence.expressions.Expression)2 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)2 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)2 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)2 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)2 ResultSetRecord (org.eclipse.persistence.internal.sessions.ResultSetRecord)2 SimpleResultSetRecord (org.eclipse.persistence.internal.sessions.SimpleResultSetRecord)2 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)2 OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)2 EntityManager (jakarta.persistence.EntityManager)1 Method (java.lang.reflect.Method)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CMPPolicy (org.eclipse.persistence.descriptors.CMPPolicy)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1