}
}
return objectFromCache;
}
DoesExistQuery existQuery = descriptor.getQueryManager().getDoesExistQuery();
// Optimize cache option to avoid executing the does exist query.
if (existQuery.shouldCheckCacheForDoesExist()) {
checkNewObjectLockVersion(clone, primaryKey, descriptor, unitOfWork);
Object registeredObject = unitOfWork.internalRegisterObject(clone, descriptor);
if (unitOfWork.hasNewObjects() && unitOfWork.getNewObjectsOriginalToClone().containsKey(clone)) {
this.mergedNewObjects.put(registeredObject, registeredObject);
}
return registeredObject;
}
// Check early return to check if it is a new object, i.e. null primary key.
Boolean doesExist = Boolean.FALSE;
if (primaryKey != null) {
doesExist = (Boolean)existQuery.checkEarlyReturn(clone, primaryKey, unitOfWork, null);
}
if (doesExist == Boolean.FALSE) {
checkNewObjectLockVersion(clone, primaryKey, descriptor, unitOfWork);
Object registeredObject = unitOfWork.internalRegisterObject(clone, descriptor);//should use cloneAndRegisterNewObject to avoid the exist check
this.mergedNewObjects.put(registeredObject, registeredObject);