Search in sources :

Example 1 with CMPPolicy

use of org.eclipse.persistence.descriptors.CMPPolicy in project eclipselink by eclipse-ee4j.

the class CMP3Policy method createPrimaryKeyFromId.

/**
 * INTERNAL:
 * Use the key to create a EclipseLink primary key.
 * If the key is simple (direct mapped) then just add it to a vector,
 * otherwise must go through the inefficient process of copying the key into the bean
 * and extracting the key from the bean.
 */
@Override
public Object createPrimaryKeyFromId(Object key, AbstractSession session) {
    // If the descriptor primary key is mapped through direct-to-field mappings,
    // then no elaborate conversion is required.
    // If key is compound, add each value to the vector.
    KeyElementAccessor[] pkElementArray = this.getKeyClassFields();
    Object[] primaryKey = null;
    if (getDescriptor().getCacheKeyType() != CacheKeyType.ID_VALUE) {
        primaryKey = new Object[pkElementArray.length];
    }
    for (int index = 0; index < pkElementArray.length; index++) {
        DatabaseMapping mapping = pkElementArray[index].getMapping();
        Object fieldValue = null;
        if (mapping.isAbstractColumnMapping()) {
            if (pkElementArray[index].isNestedAccessor()) {
                // We have nested aggregate(s) in the embedded id pkclass.
                DatabaseField keyField = pkElementArray[index].getDatabaseField();
                Object keyToUse = key;
                DatabaseMapping keyMapping = getDescriptor().getObjectBuilder().getMappingForField(keyField);
                if (keyMapping.isAggregateMapping()) {
                    keyMapping = keyMapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(keyField);
                    // Keep driving down the nested aggregates ...
                    while (keyMapping.isAggregateMapping()) {
                        keyToUse = keyMapping.getRealAttributeValueFromObject(keyToUse, session);
                        keyMapping = keyMapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(keyField);
                    }
                    fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(keyToUse, session), session);
                } else {
                    // This should never hit but just in case ... better to get a proper exception rather than a NPE etc.
                    fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(keyToUse, session), session);
                }
            } else {
                fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(pkElementArray[index].getValue(key, session), session);
            }
        } else {
            fieldValue = pkElementArray[index].getValue(key, session);
            if ((fieldValue != null) && (pkClass != null) && (mapping.isOneToOneMapping())) {
                OneToOneMapping refmapping = (OneToOneMapping) mapping;
                DatabaseField targetKey = refmapping.getSourceToTargetKeyFields().get(pkElementArray[index].getDatabaseField());
                CMPPolicy refPolicy = refmapping.getReferenceDescriptor().getCMPPolicy();
                if (refPolicy.isCMP3Policy()) {
                    Class<Object> aPKClass = refPolicy.getPKClass();
                    if ((aPKClass != null) && (aPKClass != fieldValue.getClass()) && (!aPKClass.isAssignableFrom(fieldValue.getClass()))) {
                        throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_pk_class", new Object[] { aPKClass, fieldValue.getClass() }));
                    }
                    fieldValue = ((CMP3Policy) refPolicy).getPkValueFromKeyForField(fieldValue, targetKey, session);
                }
            }
        }
        if (getDescriptor().getCacheKeyType() == CacheKeyType.ID_VALUE) {
            return fieldValue;
        }
        primaryKey[index] = fieldValue;
    }
    return new CacheId(primaryKey);
}
Also used : CMPPolicy(org.eclipse.persistence.descriptors.CMPPolicy) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 2 with CMPPolicy

use of org.eclipse.persistence.descriptors.CMPPolicy in project eclipselink by eclipse-ee4j.

the class CMP3Policy method getPkValueFromKeyForField.

/**
 * INTERNAL:
 * Pull the value for the field from the key.
 *
 * @param key Object the primary key to use to get the value for the field
 * @param field DatabaseField the field to find a value for
 * @return Object
 */
public Object getPkValueFromKeyForField(Object key, DatabaseField field, AbstractSession session) {
    Object fieldValue = null;
    KeyElementAccessor accessor = this.fieldToAccessorMap.get(field);
    DatabaseMapping mapping = accessor.getMapping();
    if (mapping.isAbstractColumnMapping()) {
        fieldValue = ((AbstractColumnMapping) mapping).getFieldValue(accessor.getValue(key, session), session);
    } else {
        fieldValue = accessor.getValue(key, session);
        if (mapping.isOneToOneMapping()) {
            OneToOneMapping refmapping = (OneToOneMapping) mapping;
            DatabaseField targetKey = refmapping.getSourceToTargetKeyFields().get(accessor.getDatabaseField());
            CMPPolicy refPolicy = refmapping.getReferenceDescriptor().getCMPPolicy();
            if (refPolicy.isCMP3Policy()) {
                Class<Object> pkClass = refPolicy.getPKClass();
                if ((pkClass != null) && (pkClass != fieldValue.getClass()) && (!pkClass.isAssignableFrom(fieldValue.getClass()))) {
                    throw new IllegalArgumentException(ExceptionLocalization.buildMessage("invalid_pk_class", new Object[] { refPolicy.getPKClass(), fieldValue.getClass() }));
                }
                fieldValue = ((CMP3Policy) refPolicy).getPkValueFromKeyForField(fieldValue, targetKey, session);
            }
        }
    }
    return fieldValue;
}
Also used : CMPPolicy(org.eclipse.persistence.descriptors.CMPPolicy) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping)

Example 3 with CMPPolicy

use of org.eclipse.persistence.descriptors.CMPPolicy in project eclipselink by eclipse-ee4j.

the class CMPDescriptorPessimisticLockingTest method setup.

@Override
protected void setup() {
    // Modify the employee project to use custom SQL queries
    EmployeeProject project = new EmployeeProject();
    ClassDescriptor employeeDescriptor = project.getDescriptors().get(Employee.class);
    ClassDescriptor addressDescriptor = project.getDescriptors().get(Address.class);
    employeeDescriptor.setCMPPolicy(new CMPPolicy());
    addressDescriptor.setCMPPolicy(new CMPPolicy());
    employeeDescriptor.getCMPPolicy().setPessimisticLockingPolicy(new PessimisticLockingPolicy());
    addressDescriptor.getCMPPolicy().setPessimisticLockingPolicy(new PessimisticLockingPolicy());
    employeeDescriptor.getCMPPolicy().getPessimisticLockingPolicy().setLockingMode(ObjectLevelReadQuery.LOCK);
    addressDescriptor.getCMPPolicy().getPessimisticLockingPolicy().setLockingMode(ObjectLevelReadQuery.LOCK_NOWAIT);
    // write project to an XML project file
    XMLProjectWriter.write(PROJECT_FILE, project);
}
Also used : CMPPolicy(org.eclipse.persistence.descriptors.CMPPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PessimisticLockingPolicy(org.eclipse.persistence.descriptors.PessimisticLockingPolicy) EmployeeProject(org.eclipse.persistence.testing.models.employee.relational.EmployeeProject)

Example 4 with CMPPolicy

use of org.eclipse.persistence.descriptors.CMPPolicy in project eclipselink by eclipse-ee4j.

the class ProjectXMLTest method setup.

@Override
public void setup() {
    employeeProject = new EmployeeProject();
    ClassDescriptor descriptor = employeeProject.getDescriptor(Employee.class);
    this.oldPolicy = descriptor.getCMPPolicy();
    descriptor.setCMPPolicy(new CMPPolicy());
}
Also used : CMPPolicy(org.eclipse.persistence.descriptors.CMPPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor)

Example 5 with CMPPolicy

use of org.eclipse.persistence.descriptors.CMPPolicy in project eclipselink by eclipse-ee4j.

the class WriteNewPessimisticLockedObjectTest method setup.

@Override
protected void setup() {
    PessimisticLockingPolicy policy = new PessimisticLockingPolicy();
    policy.setLockingMode(ObjectLevelReadQuery.LOCK_NOWAIT);
    CMPPolicy cmpPolicy = new CMPPolicy();
    cmpPolicy.setPessimisticLockingPolicy(policy);
    ClassDescriptor empDescriptor = getSession().getDescriptor(Employee.class);
    oldCMPPolicy = empDescriptor.getCMPPolicy();
    empDescriptor.setCMPPolicy(cmpPolicy);
    empDescriptor.getQueryManager().getReadObjectQuery().setLockMode(ObjectLevelReadQuery.DEFAULT_LOCK_MODE);
    this.oldLevel = empDescriptor.getUnitOfWorkCacheIsolationLevel();
    empDescriptor.setUnitOfWorkCacheIsolationLevel(ClassDescriptor.ISOLATE_CACHE_ALWAYS);
    // Flush the cache
    getSession().getIdentityMapAccessor().initializeIdentityMaps();
    getAbstractSession().beginTransaction();
}
Also used : CMPPolicy(org.eclipse.persistence.descriptors.CMPPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PessimisticLockingPolicy(org.eclipse.persistence.descriptors.PessimisticLockingPolicy)

Aggregations

CMPPolicy (org.eclipse.persistence.descriptors.CMPPolicy)9 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 PessimisticLockingPolicy (org.eclipse.persistence.descriptors.PessimisticLockingPolicy)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)3 OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)2 Method (java.lang.reflect.Method)1 PrivilegedActionException (java.security.PrivilegedActionException)1 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)1 WeavedObjectBasicIndirectionPolicy (org.eclipse.persistence.internal.indirection.WeavedObjectBasicIndirectionPolicy)1 PrivilegedGetMethod (org.eclipse.persistence.internal.security.PrivilegedGetMethod)1 ObjectReferenceMapping (org.eclipse.persistence.mappings.ObjectReferenceMapping)1 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)1 TestErrorException (org.eclipse.persistence.testing.framework.TestErrorException)1 EmployeeProject (org.eclipse.persistence.testing.models.employee.relational.EmployeeProject)1