Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
// // 30/05/2012-2.4 Guy Pelletier
// - 354678: Temp classloader is still being used during metadata processing
// 09 Jan 2013-2.5 Gordon Yorke
// - 397772: JPA 2.1 Entity Graph Support
// 08/07/2016-2.7 Dalia Abo Sheasha
// - 499335: Multiple embeddable fields can't reference same object
// 03/19/2018-2.7.2 Lukas Jungmann
// - 413120: Nested Embeddable Null pointer
package org.eclipse.persistence.mappings;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.descriptors.DescriptorEventManager;
import org.eclipse.persistence.descriptors.DescriptorQueryManager;
import org.eclipse.persistence.descriptors.changetracking.ChangeTracker;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.exceptions.OptimisticLockException;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.internal.descriptors.DescriptorIterator;
import org.eclipse.persistence.internal.descriptors.ObjectBuilder;
import org.eclipse.persistence.internal.descriptors.changetracking.AggregateAttributeChangeListener;
import org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener;
import org.eclipse.persistence.internal.helper.IdentityHashSet;
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.queries.AttributeItem;
import org.eclipse.persistence.internal.queries.JoinedAttributeManager;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.internal.security.PrivilegedClassForName;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.AggregateChangeRecord;
import org.eclipse.persistence.internal.sessions.ChangeRecord;
import org.eclipse.persistence.internal.sessions.MergeManager;
import org.eclipse.persistence.internal.sessions.ObjectChangeSet;
import org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
import org.eclipse.persistence.internal.sessions.remote.ObjectDescriptor;
import org.eclipse.persistence.queries.DeleteObjectQuery;
import org.eclipse.persistence.queries.FetchGroup;
import org.eclipse.persistence.queries.FetchGroupTracker;
import org.eclipse.persistence.queries.ObjectBuildingQuery;
import org.eclipse.persistence.queries.ObjectLevelModifyQuery;
import org.eclipse.persistence.queries.ObjectLevelReadQuery;
import org.eclipse.persistence.queries.QueryByExamplePolicy;
import org.eclipse.persistence.queries.WriteObjectQuery;
import org.eclipse.persistence.sessions.CopyGroup;
import org.eclipse.persistence.sessions.remote.DistributedSession;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.util.Map;
/**
* Purpose: Two objects can be considered to be related by aggregation if there is a strict
* 1:1 relationship between the objects. This means that if the source (parent)object exists, then
* the target (child or owned) object must exist. This class implements the behavior common to the
* aggregate object and structure mappings.
*
* @author Sati
* @since TopLink for Java 1.0
*/
public abstract class AggregateMapping extends DatabaseMapping {
/** Stores a reference class */
protected Class> referenceClass;
protected String referenceClassName;
/** The descriptor of the reference class */
protected ClassDescriptor referenceDescriptor;
/**
* Indicates whether the mapping (or at least one of its nested mappings, at any nested depth)
* references an entity.
* To return true the mapping (or nested mapping) should be ForeignReferenceMapping with non-null and non-aggregate reference descriptor.
* Lazily initialized.
*/
protected Boolean hasNestedIdentityReference;
/**
* Default constructor.
*/
protected AggregateMapping() {
super();
this.setWeight(WEIGHT_AGGREGATE);
}
/**
* Make a copy of the sourceQuery for the attribute.
*/
protected DeleteObjectQuery buildAggregateDeleteQuery(DeleteObjectQuery sourceQuery, Object sourceAttributeValue) {
DeleteObjectQuery aggregateQuery = new DeleteObjectQuery();
buildAggregateModifyQuery(sourceQuery, aggregateQuery, sourceAttributeValue);
return aggregateQuery;
}
/**
* Initialize the aggregate query with the settings from the source query.
*/
protected void buildAggregateModifyQuery(ObjectLevelModifyQuery sourceQuery, ObjectLevelModifyQuery aggregateQuery, Object sourceAttributeValue) {
// If we are map key mapping we can't build a backupAttributeValue
// from a back up clone since a map key mapping does not map a field
// on the source queries backup clone.
if (sourceQuery.getSession().isUnitOfWork() && ! isMapKeyMapping()) {
Object backupAttributeValue = getAttributeValueFromBackupClone(sourceQuery.getBackupClone());
if (backupAttributeValue == null) {
backupAttributeValue = getObjectBuilder(sourceAttributeValue, sourceQuery.getSession()).buildNewInstance();
}
aggregateQuery.setBackupClone(backupAttributeValue);
}
aggregateQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
aggregateQuery.setObject(sourceAttributeValue);
aggregateQuery.setTranslationRow(sourceQuery.getTranslationRow());
aggregateQuery.setSession(sourceQuery.getSession());
aggregateQuery.setProperties(sourceQuery.getProperties());
}
/**
* Make a copy of the sourceQuery for the attribute.
*/
protected WriteObjectQuery buildAggregateWriteQuery(WriteObjectQuery sourceQuery, Object sourceAttributeValue) {
WriteObjectQuery aggregateQuery = new WriteObjectQuery();
buildAggregateModifyQuery(sourceQuery, aggregateQuery, sourceAttributeValue);
return aggregateQuery;
}
/**
* INTERNAL:
* Clone the attribute from the clone and assign it to the backup.
*/
@Override
public void buildBackupClone(Object clone, Object backup, UnitOfWorkImpl unitOfWork) {
Object attributeValue = getAttributeValueFromObject(clone);
setAttributeValueInObject(backup, buildBackupClonePart(attributeValue, unitOfWork));
}
/**
* INTERNAL:
* Build and return a backup clone of the attribute.
*/
protected Object buildBackupClonePart(Object attributeValue, UnitOfWorkImpl unitOfWork) {
if (attributeValue == null) {
return null;
}
return getObjectBuilder(attributeValue, unitOfWork).buildBackupClone(attributeValue, unitOfWork);
}
/**
* INTERNAL:
* Clone the attribute from the original and assign it to the clone.
*/
@Override
public void buildClone(Object original, CacheKey cacheKey, Object clone, Integer refreshCascade, AbstractSession cloningSession) {
Object attributeValue = getAttributeValueFromObject(original);
setAttributeValueInObject(clone, buildClonePart(original, clone, cacheKey, attributeValue, refreshCascade, cloningSession));
}
/**
* INTERNAL:
* A combination of readFromRowIntoObject and buildClone.
*
* buildClone assumes the attribute value exists on the original and can
* simply be copied.
*
* readFromRowIntoObject assumes that one is building an original.
*
* Both of the above assumptions are false in this method, and actually
* attempts to do both at the same time.
*
* Extract value from the row and set the attribute to this value in the
* working copy clone.
* In order to bypass the shared cache when in transaction a UnitOfWork must
* be able to populate working copies directly from the row.
*/
@Override
public void buildCloneFromRow(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object clone, CacheKey sharedCacheKey, ObjectBuildingQuery sourceQuery, UnitOfWorkImpl unitOfWork, AbstractSession executionSession) {
// automatically returns a uow result from scratch that doesn't need cloning
Object cloneAttributeValue = valueFromRow(databaseRow, joinManager, sourceQuery, sharedCacheKey, executionSession, true, null);
setAttributeValueInObject(clone, cloneAttributeValue);
}
/**
* INTERNAL:
* Build and return a clone of the attribute.
*/
protected Object buildClonePart(Object original, Object clone, CacheKey cacheKey, Object attributeValue, Integer refreshCascade, AbstractSession cloningSession) {
return buildClonePart(attributeValue, clone, cacheKey, refreshCascade, cloningSession, cloningSession.isUnitOfWork() && ((UnitOfWorkImpl)cloningSession).isOriginalNewObject(original));
}
/**
* INTERNAL: * Build and return a clone of the attribute.
*/
protected Object buildClonePart(Object attributeValue, Object clone, CacheKey parentCacheKey, Integer refreshCascade, AbstractSession cloningSession, boolean isNewObject) {
if (attributeValue == null) {
return null;
}
if (cloningSession.isUnitOfWork() && isNewObject) { // only true if cloningSession is UOW as this signature only exists in this mapping.
((UnitOfWorkImpl)cloningSession).addNewAggregate(attributeValue);
}
// Do not clone for read-only.
if (cloningSession.isUnitOfWork() && cloningSession.isClassReadOnly(attributeValue.getClass())){
return attributeValue;
}
ObjectBuilder aggregateObjectBuilder = getObjectBuilder(attributeValue, cloningSession);
// bug 2612602 as we are building the working copy make sure that we call to correct clone method.
Object clonedAttributeValue = aggregateObjectBuilder.instantiateWorkingCopyClone(attributeValue, cloningSession);
aggregateObjectBuilder.populateAttributesForClone(attributeValue, parentCacheKey, clonedAttributeValue, refreshCascade, cloningSession);
//also clone the fetch group reference if applied
if (aggregateObjectBuilder.getDescriptor().hasFetchGroupManager()) {
aggregateObjectBuilder.getDescriptor().getFetchGroupManager().copyAggregateFetchGroupInto(attributeValue, clonedAttributeValue, clone, cloningSession);
}
return clonedAttributeValue;
}
/**
* INTERNAL:
* Copy of the attribute of the object.
* This is NOT used for unit of work but for templatizing an object.
*/
@Override
public void buildCopy(Object copy, Object original, CopyGroup group) {
Object attributeValue = getAttributeValueFromObject(original);
setAttributeValueInObject(copy, buildCopyOfAttributeValue(attributeValue, group));
}
/**
* Copy of the attribute of the object.
* This is NOT used for unit of work but for templatizing an object.
*/
protected Object buildCopyOfAttributeValue(Object attributeValue, CopyGroup group) {
if (attributeValue == null) {
return null;
}
return getObjectBuilder(attributeValue, group.getSession()).copyObject(attributeValue, group);
}
/**
* INTERNAL:
* In case Query By Example is used, this method generates an expression from a attribute value pair. Since
* this is an Aggregate mapping, a recursive call is made to the buildExpressionFromExample method of
* ObjectBuilder.
*/
@Override
public Expression buildExpression(Object queryObject, QueryByExamplePolicy policy, Expression expressionBuilder, Map processedObjects, AbstractSession session) {
String attributeName = this.getAttributeName();
Object attributeValue = this.getRealAttributeValueFromObject(queryObject, session);
if (!policy.shouldIncludeInQuery(queryObject.getClass(), attributeName, attributeValue)) {
//the attribute name and value pair is not to be included in the query.
return null;
}
if (attributeValue == null) {
//even though it is null, it is to be always included in the query
Expression expression = expressionBuilder.get(attributeName);
return policy.completeExpressionForNull(expression);
}
ObjectBuilder objectBuilder = getReferenceDescriptor().getObjectBuilder();
return objectBuilder.buildExpressionFromExample(attributeValue, policy, expressionBuilder.get(attributeName), processedObjects, session);
}
/**
* INTERNAL:
* Build and return a new instance of the specified attribute.
* This will be populated by a merge.
*/
protected Object buildNewMergeInstanceOf(Object sourceAttributeValue, AbstractSession session) {
return getObjectBuilder(sourceAttributeValue, session).buildNewInstance();
}
/*
* INTERNAL:
* Cascade perform delete through mappings that require the cascade
*/
// public void cascadePerformDeleteIfRequired(Object object, UnitOfWork uow, Map visitedObjects){
//objects referenced by this mapping are not registered as they have
// no identity, this is a no-op.
// }
/*
* INTERNAL:
* Cascade registerNew for Create through mappings that require the cascade
*/
// public void cascadeRegisterNewIfRequired(Object object, UnitOfWork uow, Map visitedObjects){
//aggregate objects are not registered as they have no identity, this is a no-op.
// }
/**
* INTERNAL:
* Compare the attributes. Return true if they are alike.
*/
protected boolean compareAttributeValues(Object attributeValue1, Object attributeValue2, AbstractSession session) {
if ((attributeValue1 == null) && (attributeValue2 == null)) {
return true;
}
if ((attributeValue1 == null) || (attributeValue2 == null)) {
return false;
}
if (attributeValue1.getClass() != attributeValue2.getClass()) {
return false;
}
return getObjectBuilder(attributeValue1, session).compareObjects(attributeValue1, attributeValue2, session);
}
/**
* INTERNAL:
* Compare the changes between two aggregates.
* Return a change record holding the changes.
*/
@Override
public ChangeRecord compareForChange(Object clone, Object backup, ObjectChangeSet owner, AbstractSession session) {
Object cloneAttribute = getAttributeValueFromObject(clone);
Object backupAttribute = null;
if (!owner.isNew()) {
backupAttribute = getAttributeValueFromObject(backup);
if ((cloneAttribute == null) && (backupAttribute == null)) {
return null;// no change
}
if ((cloneAttribute != null) && (backupAttribute != null) && (!cloneAttribute.getClass().equals(backupAttribute.getClass()))) {
backupAttribute = null;
}
}
AggregateChangeRecord changeRecord = new AggregateChangeRecord(owner);
changeRecord.setAttribute(getAttributeName());
changeRecord.setMapping(this);
changeRecord.setOldValue(backupAttribute);
if (cloneAttribute == null) {// the attribute was set to null
changeRecord.setChangedObject(null);
return changeRecord;
}
ObjectBuilder builder = getObjectBuilder(cloneAttribute, session);
//if the owner is new then the backup will be null, if the owner is new then the aggregate is new
//if the backup is null but the owner is not new then this aggregate is new
ObjectChangeSet initialChanges = builder.createObjectChangeSet(cloneAttribute, (UnitOfWorkChangeSet)owner.getUOWChangeSet(), (backupAttribute == null), session);
ObjectChangeSet changeSet = builder.compareForChange(cloneAttribute, backupAttribute, (UnitOfWorkChangeSet)owner.getUOWChangeSet(), session);
if (changeSet == null) {
if (initialChanges.isNew()) {
// This happens if original aggregate is of class A, the new aggregate
// is of class B (B inherits from A) - and neither A nor B has any mapped attributes.
// CR3664
changeSet = initialChanges;
} else {
return null;// no change
}
}
changeRecord.setChangedObject(changeSet);
return changeRecord;
}
/**
* INTERNAL:
* Compare the attributes belonging to this mapping for the objects.
*/
@Override
public boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session) {
return compareAttributeValues(getAttributeValueFromObject(firstObject), getAttributeValueFromObject(secondObject), session);
}
/**
* INTERNAL:
* Convert all the class-name-based settings in this mapping to actual class-based
* settings. This method is used when converting a project that has been built
* with class names to a project with classes.
*/
@Override
public void convertClassNamesToClasses(ClassLoader classLoader) {
super.convertClassNamesToClasses(classLoader);
if (getReferenceClassName() != null) {
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
setReferenceClass(AccessController.doPrivileged(new PrivilegedClassForName<>(getReferenceClassName(), true, classLoader)));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(getReferenceClassName(), exception.getException());
}
} else {
setReferenceClass(org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getReferenceClassName(), true, classLoader));
}
} catch (ClassNotFoundException exc) {
throw ValidationException.classNotFoundWhileConvertingClassNames(getReferenceClassName(), exc);
}
}
}
/**
* INTERNAL:
* Execute a descriptor event for the specified event code.
*/
protected void executeEvent(int eventCode, ObjectLevelModifyQuery query) {
ClassDescriptor referenceDescriptor = getReferenceDescriptor(query.getObject(), query.getSession());
// PERF: Avoid events if no listeners.
if (referenceDescriptor.getEventManager().hasAnyEventListeners()) {
referenceDescriptor.getEventManager().executeEvent(new org.eclipse.persistence.descriptors.DescriptorEvent(eventCode, query));
}
}
/**
* INTERNAL:
* An object has been serialized from the server to the remote client.
* Replace the transient attributes of the remote value holders
* with client-side objects.
*/
protected void fixAttributeValue(Object attributeValue, Map