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.
*
* 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
// 11/10/2011-2.4 Guy Pelletier
// - 357474: Address primaryKey option from tenant discriminator column
// 30/05/2012-2.4 Guy Pelletier
// - 354678: Temp classloader is still being used during metadata processing
// 06/03/2013-2.5.1 Guy Pelletier
// - 402380: 3 jpa21/advanced tests failed on server with
// "java.lang.NoClassDefFoundError: org/eclipse/persistence/testing/models/jpa21/advanced/enums/Gender"
package org.eclipse.persistence.mappings;
import org.eclipse.persistence.core.mappings.CoreMapping;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.indirection.ValueHolderInterface;
import org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor;
import org.eclipse.persistence.internal.databaseaccess.DatabasePlatform;
import org.eclipse.persistence.internal.descriptors.ClassNameConversionRequired;
import org.eclipse.persistence.internal.descriptors.DescriptorIterator;
import org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor;
import org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor;
import org.eclipse.persistence.internal.expressions.ObjectExpression;
import org.eclipse.persistence.internal.expressions.QueryKeyExpression;
import org.eclipse.persistence.internal.helper.ConversionManager;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.helper.DatabaseTable;
import org.eclipse.persistence.internal.helper.IdentityHashSet;
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.indirection.DatabaseValueHolder;
import org.eclipse.persistence.internal.queries.AttributeItem;
import org.eclipse.persistence.internal.queries.ContainerPolicy;
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.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.internal.sessions.remote.RemoteSessionController;
import org.eclipse.persistence.internal.sessions.remote.RemoteValueHolder;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.queries.DeleteObjectQuery;
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.Project;
import org.eclipse.persistence.sessions.remote.DistributedSession;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
Purpose: Defines how an attribute of an object maps to and from the database
*
*
Responsibilities:
*
Define type of relationship (1:1/1:M/M:M/etc.)
*
Define instance variable name and fields names required
*
Define any additional properties (ownership, indirection, read only, etc.)
*
Control building the value for the instance variable from the database row
*
Control building the database fields from the object
*
Control any pre/post updating/inserting/deleting required to maintain the relationship
*
Merges object changes for unit of work.
*
Clones objects for unit of work.
*
cache computed information to optimize performance
*
*
* @author Sati
* @since TOPLink/Java 1.0
*/
public abstract class DatabaseMapping extends CoreMapping implements Cloneable, Serializable {
public enum WriteType { INSERT, UPDATE, UNDEFINED }
/** Used to reduce memory for mappings with no fields. */
protected static final List NO_FIELDS = Collections.emptyList();
/** Used to share integer instance to reduce memory. */
protected static final Integer NO_WEIGHT = Integer.MAX_VALUE;
protected static final Integer WEIGHT_DIRECT = 1;
protected static final Integer WEIGHT_TRANSFORM = 100;
protected static final Integer WEIGHT_AGGREGATE = 200;
protected static final Integer WEIGHT_TO_ONE = 400;
/** ClassDescriptor to which this mapping belongs to */
protected ClassDescriptor descriptor;
/** Wrapper to store the reference objects. */
protected AttributeAccessor attributeAccessor;
/** Makes this mapping read only. No write are performed on it. Default is false */
protected boolean isReadOnly;
/** Specifies whether this mapping is optional (i.e. field may be null). Used for DDL generation. */
protected boolean isOptional;
/** Specifies whether this mapping is lazy, this means not included in the default fetch group. */
protected Boolean isLazy;
/** Fields associated with the mappings are cached */
protected List fields;
/** It is needed only in remote initialization and mapping is in parent descriptor */
protected boolean isRemotelyInitialized;
/** This is a TopLink defined attribute that allows us to sort the mappings */
protected Integer weight = NO_WEIGHT;
/** Allow user defined properties. */
protected Map properties;
/** Allow the user to defined un-converted properties which will be initialized at runtime. */
protected Map> unconvertedProperties;
/**
* Used by the CMP3Policy to see if this mapping should be used in
* processing pk classes for find methods
*/
protected boolean derivesId;
/**
*
*/
protected boolean isJPAId = false;
/**
* A mapsId value.
*/
protected String mapsIdValue;
/**
* The id mapping this mapping derives. Used by the CMP3Policy to see if
* this mapping should be used in processing pk classes for find methods.
*/
protected DatabaseMapping derivedIdMapping;
/**
* PERF: Used as a quick check to see if this mapping is a primary key mapping,
* set by the object builder during initialization.
*/
protected boolean isPrimaryKeyMapping = false;
/**
* PERF: Cache the mappings attribute name.
*/
protected String attributeName;
/**
* Records if this mapping is being used as a MapKeyMapping. This is important for recording main mappings
*/
protected boolean isMapKeyMapping = false;
//used by the object build/merge code to control building/merging into the
//shared cache.
protected boolean isCacheable = true;
/**
* Irrelevant (and not set) unless descriptor has SerializedObjectPolicy (SOP).
* If descriptor has SOP, then ObjectLevelReadQuery (with shouldUseSerializedObjectPolicy flag set to true)
* reads in row that contain both field/value pairs and sopObject.
* This flag indicates whether the data for this mapping is contained in the row's sopObject or in fields/values.
* Boolean.TRUE - sopObject (in sopObject)
* Boolean.FALSE - fields/values (out sopObject);
* null - both sopObject and fields/values (both in and out sopObject).
* While writing to the data base the mapping will be used for writing into sopObject unless this flag is set to Boolean.FALSE;
*/
protected Boolean isInSopObject;
/**
* PUBLIC:
* Default constructor.
*/
protected DatabaseMapping() {
this.isOptional = true;
this.isReadOnly = false;
this.attributeAccessor = new InstanceVariableAttributeAccessor();
}
/**
* PUBLIC:
* Add an unconverted property (to be initialiazed at runtime)
*/
public void addUnconvertedProperty(String propertyName, String propertyValue, String propertyType) {
List valuePair = new ArrayList<>(2);
valuePair.add(propertyValue);
valuePair.add(propertyType);
getUnconvertedProperties().put(propertyName, valuePair);
}
/**
* INTERNAL:
* Clone the attribute from the clone and assign it to the backup.
*
*/
public abstract void buildBackupClone(Object clone, Object backup, UnitOfWorkImpl unitOfWork);
/**
* INTERNAL:
* Require for cloning, the part must be cloned.
*/
public Object buildBackupCloneForPartObject(Object attributeValue, Object clone, Object backup, UnitOfWorkImpl unitOfWork) {
throw DescriptorException.invalidMappingOperation(this, "buildBackupCloneForPartObject");
}
/**
* INTERNAL:
* Clone the attribute from the original and assign it to the clone.
*/
public abstract void buildClone(Object original, CacheKey cacheKey, Object clone, Integer refreshCascade, AbstractSession 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.
*/
public abstract void buildCloneFromRow(AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object clone, CacheKey sharedCacheKey, ObjectBuildingQuery sourceQuery, UnitOfWorkImpl unitOfWork, AbstractSession executionSession);
/**
* INTERNAL:
* Builds a shallow original object. Only direct attributes and primary
* keys are populated. In this way the minimum original required for
* instantiating a working copy clone can be built without placing it in
* the shared cache (no concern over cycles).
*/
public void buildShallowOriginalFromRow(AbstractRecord databaseRow, Object original, JoinedAttributeManager joinManager, ObjectBuildingQuery query, AbstractSession executionSession) {
return;
}
/**
* INTERNAL:
* Require for cloning, the part must be cloned.
*/
public Object buildCloneForPartObject(Object attributeValue, Object original, CacheKey cacheKey, Object clone, AbstractSession cloningSession, Integer refreshCascade, boolean isExisting, boolean isFromSharedCache) {
throw DescriptorException.invalidMappingOperation(this, "buildCloneForPartObject");
}
/**
* INTERNAL:
* Performs a first level clone of the attribute. This generally means on the container will be cloned.
*/
public Object buildContainerClone(Object attributeValue, AbstractSession cloningSession){
return attributeValue;
}
/**
* INTERNAL:
* Copy of the attribute of the object.
* This is NOT used for unit of work but for templatizing an object.
*/
public void buildCopy(Object copy, Object original, CopyGroup group) {
}
/**
* INTERNAL:
* In case Query By Example is used, this method builds and returns an expression that
* corresponds to a single attribue and it's value.
*/
public Expression buildExpression(Object queryObject, QueryByExamplePolicy policy, Expression expressionBuilder, Map processedObjects, AbstractSession session) {
if (policy.shouldValidateExample()){
throw QueryException.unsupportedMappingQueryByExample(queryObject.getClass().getName(), this);
}
return null;
}
/**
* INTERNAL:
* Used to allow object level comparisons.
*/
public Expression buildObjectJoinExpression(Expression base, Object value, AbstractSession session) {
throw QueryException.unsupportedMappingForObjectComparison(this, base);
}
/**
* INTERNAL:
* Used to allow object level comparisons.
*/
public Expression buildObjectJoinExpression(Expression base, Expression argument, AbstractSession session) {
throw QueryException.unsupportedMappingForObjectComparison(this, base);
}
/**
* INTERNAL:
* Cascade registerNew for Create through mappings that require the cascade
*/
abstract public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects);
/**
* INTERNAL:
* Cascade removal of orphaned private owned objects from the UnitOfWorkChangeSet
*/
public void cascadePerformRemovePrivateOwnedObjectFromChangeSetIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects) {
// no-op by default
}
/**
* INTERNAL:
* Cascade registerNew for Create through mappings that require the cascade
*/
abstract public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, Map visitedObjects);
/**
* INTERNAL:
* Cascade discover and persist new objects during commit.
*/
public void cascadeDiscoverAndPersistUnregisteredNewObjects(Object object, Map newObjects, Map unregisteredExistingObjects, Map visitedObjects, UnitOfWorkImpl uow, Set cascadeErrors) {
// Do nothing by default, (direct and xml mappings do not require anything).
}
/**
* INTERNAL:
* Used by AttributeLevelChangeTracking to update a changeRecord with calculated changes
* as apposed to detected changes. If an attribute can not be change tracked it's
* changes can be detected through this process.
*/
public void calculateDeferredChanges(ChangeRecord changeRecord, AbstractSession session){
throw DescriptorException.invalidMappingOperation(this, "calculatedDeferredChanges");
}
/**
* INTERNAL:
* Clones itself.
*
* @return new instance of itself
*/
@Override
public Object clone() {
// Bug 3037701 - clone the AttributeAccessor
DatabaseMapping mapping = null;
try {
mapping = (DatabaseMapping)super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
mapping.setAttributeAccessor((AttributeAccessor)attributeAccessor.clone());
return mapping;
}
/**
* INTERNAL:
* Helper method to clone a list of fields (used in aggregate initialization cloning).
*/
protected List cloneFields(List fields) {
List clonedFields = new ArrayList<>(fields.size());
for (Iterator iterator = fields.iterator(); iterator.hasNext();) {
clonedFields.add((iterator.next()).clone());
}
return clonedFields;
}
/**
* This method must be overwritten in the subclasses to return a collection of all the
* fields this mapping represents.
*/
protected List collectFields() {
return NO_FIELDS;
}
/**
* INTERNAL:
* This method is used to store the FK fields that can be cached that correspond to noncacheable mappings
* the FK field values will be used to re-issue the query when cloning the shared cache entity
*/
public void collectQueryParameters(Set record){
//no-op for mappings that do not support PROTECTED cache isolation
}
/**
* INTERNAL:
* Mapping callback for post-initialization of source and target expression fields
* created when a mapping's selectionCriteria is created early with uninitialized fields.
* @see OneToOneMapping#postInitializeSourceAndTargetExpressions()
* @see OneToManyMapping#postInitializeSourceAndTargetExpressions()
*/
public void postInitializeSourceAndTargetExpressions() {
// no-op by default
// EL Bug 426500
}
/**
* INTERNAL:
* This method was created in VisualAge.
*/
abstract public ChangeRecord compareForChange(Object clone, Object backup, ObjectChangeSet owner, AbstractSession session);
/**
* INTERNAL:
* Compare the attributes belonging to this mapping for the objects.
*/
public abstract boolean compareObjects(Object firstObject, Object secondObject, AbstractSession session);
/**
* INTERNAL:
* Convert all the class-name-based settings in this mapping to actual class-based
* settings
* This method is implemented by subclasses as necessary.
*/
public void convertClassNamesToClasses(ClassLoader classLoader) {
if (hasUnconvertedProperties()) {
for (String propertyName : getUnconvertedProperties().keySet()) {
List valuePair = getUnconvertedProperties().get(propertyName);
String value = valuePair.get(0);
String valueTypeName = valuePair.get(1);
Class> valueType = null;
// Have to initialize the valueType now
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
valueType = AccessController.doPrivileged(new PrivilegedClassForName<>(valueTypeName, true, classLoader));
} catch (PrivilegedActionException exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(valueTypeName, exception.getException());
}
} else {
valueType = PrivilegedAccessHelper.getClassForName(valueTypeName, true, classLoader);
}
} catch (Exception exception) {
throw ValidationException.classNotFoundWhileConvertingClassNames(valueTypeName, exception);
}
// Add the converted property. If the value type is the same
// as the source (value) type, no conversion is made.
getProperties().put(propertyName, ConversionManager.getDefaultManager().convertObject(value, valueType));
}
}
}
/**
* Convenience method to ensure converters have an opportunity to convert
* any class names to classes during project setup.
*/
protected void convertConverterClassNamesToClasses(Converter converter, ClassLoader classLoader) {
if (converter instanceof ClassNameConversionRequired) {
((ClassNameConversionRequired)converter).convertClassNamesToClasses(classLoader);
}
}
/**
* INTERNAL:
* Builder the unit of work value holder.
*
* @param buildDirectlyFromRow indicates that we are building the clone directly
* from a row as opposed to building the original from the row, putting it in
* the shared cache, and then cloning the original.
*/
public DatabaseValueHolder createCloneValueHolder(ValueHolderInterface attributeValue, Object original, Object clone, AbstractRecord row, AbstractSession cloningSession, boolean buildDirectlyFromRow) {
throw DescriptorException.invalidMappingOperation(this, "createUnitOfWorkValueHolder");
}
/**
* ADVANCED:
* Returns true if the mapping references a JPA ID attribute for the CMP3Policy and JPA ID classes.
*/
public boolean derivesId() {
return derivesId;
}
/**
* INTERNAL:
* This method is called to update collection tables prior to commit.
*/
public void earlyPreDelete(DeleteObjectQuery query, Object object) {
}
/**
* INTERNAL:
* Extract the nested attribute expressions that apply to this mapping.
* This is used for partial objects, and batch fetching.
*/
protected List extractNestedExpressions(List expressions, ExpressionBuilder newRoot) {
List nestedExpressions = new ArrayList<>(expressions.size());
/*
* If the expression closest to to the Builder is for this mapping, that expression is rebuilt using
* newRoot and added to the nestedExpressions list.
*/
for (Expression next : expressions) {
// The expressionBuilder can be one of the locked expressions in
// the ForUpdateOfClause.
if (!next.isQueryKeyExpression()) {
continue;
}
QueryKeyExpression expression = (QueryKeyExpression)next;
ObjectExpression base = expression;
boolean afterBase = false;
while (!base.getBaseExpression().isExpressionBuilder()) {
base = (ObjectExpression)base.getBaseExpression();
afterBase = true;
}
if (base.getName().equals(getAttributeName())) {
// Only add the nested expressions for the mapping (not the mapping itself).
if (afterBase) {
nestedExpressions.add(expression.rebuildOn(base, newRoot));
}
}
}
return nestedExpressions;
}
/**
* INTERNAL:
* Extract the nested attribute expressions that apply to this mapping.
* This is used for joining, and locking.
* For aggregates return the nested foreign reference mapping, not the aggregate, as the aggregates are not joined,
* and share their parent's query.
* @param rootExpressionsAllowed true if newRoot itself can be one of the
* expressions returned (used for locking)
*/
protected List extractNestedNonAggregateExpressions(List expressions, ExpressionBuilder newRoot, boolean rootExpressionsAllowed) {
List nestedExpressions = new ArrayList<>(expressions.size());
/*
* need to work on all expressions with at least 2 nestings off the base expression builder, excluding
* aggregateObjectMapping expressions from the count (only ForeignReferenceMapping expressions count). For those
* expressions, If the expression closest to to the Builder is for this mapping, that expression is rebuilt using
* newRoot and added to the nestedExpressions list.
*/
for (Expression next : expressions) {
// The expressionBuilder can be one of the locked expressions in
// the ForUpdateOfClause.
if (!next.isQueryKeyExpression()) {
continue;
}
QueryKeyExpression expression = (QueryKeyExpression)next;
ObjectExpression base = expression;
boolean afterBase = false;
boolean done = false;
ObjectExpression prevExpression = base;
while (!base.getBaseExpression().isExpressionBuilder()&& !done) {
base = (ObjectExpression)base.getBaseExpression();
while (!base.isExpressionBuilder() && (base.getMapping() != null && base.getMapping().isAggregateObjectMapping())) {
base = (ObjectExpression)base.getBaseExpression();
}
if (base.isExpressionBuilder()){
done = true;
//use the one closest to the expression builder that wasn't an aggregate
base = prevExpression;
} else {
prevExpression = base;
afterBase = true;
}
}
if (afterBase && base.getName().equals(getAttributeName())) {
nestedExpressions.add(expression.rebuildOn(base, newRoot));
} else if (rootExpressionsAllowed && expression.getBaseExpression().isExpressionBuilder() && expression.getName().equals(getAttributeName())) {
nestedExpressions.add(newRoot);
}
}
return nestedExpressions;
}
/**
* INTERNAL:
* If there is root expression in the list then indicates whether it shouldUseOuterJoin,
* otherwise return false.
*/
protected boolean hasRootExpressionThatShouldUseOuterJoin(List expressions) {
for (Iterator expressionsEnum = expressions.iterator();
expressionsEnum.hasNext();) {
Expression next = (Expression)expressionsEnum.next();
// The expressionBuilder can be one of the locked expressions in
// the ForUpdateOfClause.
if (!next.isQueryKeyExpression()) {
continue;
}
QueryKeyExpression expression = (QueryKeyExpression)next;
if (expression.getBaseExpression().isExpressionBuilder() && expression.getName().equals(getAttributeName())) {
return expression.shouldUseOuterJoin();
}
}
return false;
}
/**
* INTERNAL:
* Used to store un-converted properties, which are subsequently converted
* at runtime (through the convertClassNamesToClasses method).
*/
public boolean hasUnconvertedProperties() {
return unconvertedProperties != null;
}
/**
* INTERNAL:
* An object has been serialized from the server to the client.
* Replace the transient attributes of the remote value holders
* with client-side objects.
*/
public abstract void fixObjectReferences(Object object, Map