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);
}
}
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;
}
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);
}
}
}
}
}
}
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);
}
}
}
}
}
}
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;
}
Aggregations