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