Search in sources :

Example 1 with DoesExistQuery

use of org.eclipse.persistence.queries.DoesExistQuery in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method checkForUnregisteredExistingObject.

/**
 * INTERNAL:
 * Return if the object is an existing object (but has not been registered),
 * or a new object (that has not be persisted).
 */
public boolean checkForUnregisteredExistingObject(Object object) {
    ClassDescriptor descriptor = getDescriptor(object.getClass());
    Object primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, this, true);
    if (primaryKey == null) {
        return false;
    }
    DoesExistQuery existQuery = descriptor.getQueryManager().getDoesExistQuery();
    existQuery = (DoesExistQuery) existQuery.clone();
    existQuery.setObject(object);
    existQuery.setPrimaryKey(primaryKey);
    existQuery.setDescriptor(descriptor);
    existQuery.setIsExecutionClone(true);
    return (Boolean) executeQuery(existQuery);
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery)

Example 2 with DoesExistQuery

use of org.eclipse.persistence.queries.DoesExistQuery in project eclipselink by eclipse-ee4j.

the class ObjectPersistenceRuntimeXMLProject method buildQueryManagerDescriptor.

protected ClassDescriptor buildQueryManagerDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(DescriptorQueryManager.class);
    descriptor.setDefaultRootElement("query-policy");
    descriptor.getInheritancePolicy().setClassIndicatorField(new XMLField("@xsi:type"));
    descriptor.getInheritancePolicy().addClassIndicator(DescriptorQueryManager.class, getPrimaryNamespaceXPath() + "query-policy");
    XMLCompositeCollectionMapping namedQueriesMapping = new XMLCompositeCollectionMapping();
    namedQueriesMapping.setReferenceClass(DatabaseQuery.class);
    namedQueriesMapping.useCollectionClass(Vector.class);
    namedQueriesMapping.setAttributeName("queries");
    namedQueriesMapping.setGetMethodName("getAllQueries");
    namedQueriesMapping.setSetMethodName("setAllQueries");
    namedQueriesMapping.setXPath(getSecondaryNamespaceXPath() + "queries/" + getSecondaryNamespaceXPath() + "query");
    descriptor.addMapping(namedQueriesMapping);
    XMLDirectMapping queryTimeoutMapping = new XMLDirectMapping();
    queryTimeoutMapping.setAttributeName("queryTimeout");
    queryTimeoutMapping.setGetMethodName("getQueryTimeout");
    queryTimeoutMapping.setSetMethodName("setQueryTimeout");
    queryTimeoutMapping.setXPath(getPrimaryNamespaceXPath() + "timeout/text()");
    queryTimeoutMapping.setNullValue(DescriptorQueryManager.DefaultTimeout);
    descriptor.addMapping(queryTimeoutMapping);
    XMLCompositeObjectMapping insertQueryMapping = new XMLCompositeObjectMapping();
    insertQueryMapping.setAttributeName("insertQuery");
    insertQueryMapping.setGetMethodName("getInsertQuery");
    insertQueryMapping.setSetMethodName("setInsertQuery");
    insertQueryMapping.setXPath(getPrimaryNamespaceXPath() + "insert-query");
    insertQueryMapping.setReferenceClass(InsertObjectQuery.class);
    descriptor.addMapping(insertQueryMapping);
    XMLCompositeObjectMapping updateQueryMapping = new XMLCompositeObjectMapping();
    updateQueryMapping.setAttributeName("updateQuery");
    updateQueryMapping.setGetMethodName("getUpdateQuery");
    updateQueryMapping.setSetMethodName("setUpdateQuery");
    updateQueryMapping.setXPath(getPrimaryNamespaceXPath() + "update-query");
    updateQueryMapping.setReferenceClass(UpdateObjectQuery.class);
    descriptor.addMapping(updateQueryMapping);
    XMLCompositeObjectMapping deleteQueryMapping = new XMLCompositeObjectMapping();
    deleteQueryMapping.setAttributeName("deleteQuery");
    deleteQueryMapping.setGetMethodName("getDeleteQuery");
    deleteQueryMapping.setSetMethodName("setDeleteQuery");
    deleteQueryMapping.setXPath(getPrimaryNamespaceXPath() + "delete-query");
    deleteQueryMapping.setReferenceClass(DeleteObjectQuery.class);
    descriptor.addMapping(deleteQueryMapping);
    XMLCompositeObjectMapping doesExistQueryMapping = new XMLCompositeObjectMapping();
    doesExistQueryMapping.setAttributeName("doesExistQuery");
    // Handle translation of default does-exist to null.
    doesExistQueryMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public Object getAttributeValueFromObject(Object object) {
            DoesExistQuery query = ((DescriptorQueryManager) object).getDoesExistQuery();
            if ((!query.isCallQuery()) && query.shouldCheckCacheForDoesExist()) {
                return null;
            }
            return query;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            DoesExistQuery query = (DoesExistQuery) value;
            if (value == null) {
                return;
            }
            ((DescriptorQueryManager) object).setDoesExistQuery(query);
        }
    });
    doesExistQueryMapping.setXPath(getPrimaryNamespaceXPath() + "does-exist-query");
    doesExistQueryMapping.setReferenceClass(DoesExistQuery.class);
    descriptor.addMapping(doesExistQueryMapping);
    XMLCompositeObjectMapping readObjectQueryMapping = new XMLCompositeObjectMapping();
    readObjectQueryMapping.setAttributeName("readObjectQuery");
    readObjectQueryMapping.setGetMethodName("getReadObjectQuery");
    readObjectQueryMapping.setSetMethodName("setReadObjectQuery");
    readObjectQueryMapping.setXPath(getPrimaryNamespaceXPath() + "read-object-query");
    readObjectQueryMapping.setReferenceClass(ReadObjectQuery.class);
    descriptor.addMapping(readObjectQueryMapping);
    XMLCompositeObjectMapping readAllQueryMapping = new XMLCompositeObjectMapping();
    readAllQueryMapping.setAttributeName("readAllQuery");
    readAllQueryMapping.setGetMethodName("getReadAllQuery");
    readAllQueryMapping.setSetMethodName("setReadAllQuery");
    readAllQueryMapping.setXPath(getPrimaryNamespaceXPath() + "read-all-query");
    readAllQueryMapping.setReferenceClass(ReadAllQuery.class);
    descriptor.addMapping(readAllQueryMapping);
    return descriptor;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 3 with DoesExistQuery

use of org.eclipse.persistence.queries.DoesExistQuery in project eclipselink by eclipse-ee4j.

the class AbstractSession method doesObjectExist.

/**
 * PUBLIC:
 * Return if the object exists on the database or not.
 * This always checks existence on the database.
 */
@Override
public boolean doesObjectExist(Object object) throws DatabaseException {
    DoesExistQuery query = new DoesExistQuery();
    query.setObject(object);
    query.checkDatabaseForDoesExist();
    query.setIsExecutionClone(true);
    return (Boolean) executeQuery(query);
}
Also used : DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery)

Example 4 with DoesExistQuery

use of org.eclipse.persistence.queries.DoesExistQuery in project eclipselink by eclipse-ee4j.

the class QueryManagerHasDoesExistQueryIsSQLCallQueryTest method setup.

@Override
protected void setup() {
    getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    descriptorToModify = project.getDescriptors().get(Employee.class);
    DoesExistQuery testReadQuery = new DoesExistQuery();
    SQLCall testCall = new SQLCall();
    // setting the SQLCall on ReadObjectQuery
    testReadQuery.setCall(testCall);
    testReadQuery.setSQLString("testString");
    descriptorToModify.getQueryManager().setDoesExistQuery(testReadQuery);
}
Also used : SQLCall(org.eclipse.persistence.queries.SQLCall) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery)

Example 5 with DoesExistQuery

use of org.eclipse.persistence.queries.DoesExistQuery in project eclipselink by eclipse-ee4j.

the class MergeManager method registerObjectForMergeCloneIntoWorkingCopy.

/**
 * INTERNAL:
 * When merging from a clone when the cache cannot be guaranteed the object must be first read if it is existing
 * and not in the cache. Otherwise no changes will be detected as the original state is missing.
 */
protected Object registerObjectForMergeCloneIntoWorkingCopy(Object clone, boolean shouldForceCascade) {
    UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) this.session;
    ClassDescriptor descriptor = unitOfWork.getDescriptor(clone.getClass());
    Object primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(clone, unitOfWork, true);
    // Must use the java class as this may be a bean that we are merging and it may not have the same class as the
    // objects in the cache.  As of EJB 2.0.
    Object objectFromCache = null;
    if (primaryKey != null) {
        objectFromCache = unitOfWork.getIdentityMapAccessorInstance().getFromIdentityMap(primaryKey, null, descriptor.getJavaClass(), false, descriptor);
    }
    if (objectFromCache == null) {
        // Ensure we return the working copy if this has already been registered.
        objectFromCache = unitOfWork.checkIfAlreadyRegistered(clone, descriptor);
    }
    if (objectFromCache != null) {
        // gf830 - merging a removed entity should throw exception.
        if (!isForRefresh && unitOfWork.isObjectDeleted(objectFromCache)) {
            if (shouldMergeCloneIntoWorkingCopy() || shouldMergeCloneWithReferencesIntoWorkingCopy()) {
                throw new IllegalArgumentException(ExceptionLocalization.buildMessage("cannot_merge_removed_entity", new Object[] { clone }));
            }
        }
        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, false);
        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);
        // should use cloneAndRegisterNewObject to avoid the exist check
        Object registeredObject = unitOfWork.internalRegisterObject(clone, descriptor, shouldForceCascade);
        this.mergedNewObjects.put(registeredObject, registeredObject);
        return registeredObject;
    }
    // Otherwise it is existing and not in the cache so it must be read.
    Object object = unitOfWork.readObject(clone);
    if (object == null) {
        checkNewObjectLockVersion(clone, primaryKey, descriptor, unitOfWork);
        // bug6180972: avoid internal register's existence check and be sure to put the new object in the mergedNewObjects collection
        object = unitOfWork.cloneAndRegisterNewObject(clone, shouldForceCascade);
        this.mergedNewObjects.put(object, object);
    }
    return object;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery)

Aggregations

DoesExistQuery (org.eclipse.persistence.queries.DoesExistQuery)9 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 DescriptorEvent (org.eclipse.persistence.descriptors.DescriptorEvent)1 ObjectBuilder (org.eclipse.persistence.internal.descriptors.ObjectBuilder)1 CacheKey (org.eclipse.persistence.internal.identitymaps.CacheKey)1 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)1 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)1 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)1 XMLField (org.eclipse.persistence.oxm.XMLField)1 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)1 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)1 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)1 SQLCall (org.eclipse.persistence.queries.SQLCall)1 Employee (org.eclipse.persistence.testing.models.employee.domain.Employee)1