Search in sources :

Example 1 with DeleteObjectQuery

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

the class QueryManagerHasDeleteQueryIsSQLCallQueryTest method setup.

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

Example 2 with DeleteObjectQuery

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

the class UnitOfWorkImpl method commitToDatabase.

/**
 * INTERNAL:
 * CommitChanges To The Database from a calculated changeSet
 * @param commitTransaction false if called by writeChanges as intent is
 * not to finalize the transaction.
 */
protected void commitToDatabase(boolean commitTransaction) {
    try {
        // CR4202 - ported from 3.6.4
        if (wasTransactionBegunPrematurely()) {
            // beginTransaction() has been already called
            setWasTransactionBegunPrematurely(false);
        } else {
            beginTransaction();
        }
        if (commitTransaction) {
            setWasNonObjectLevelModifyQueryExecuted(false);
        }
        this.preDeleteComplete = false;
        // PERF: Avoid deletion if nothing to delete.
        List deletedObjects = null;
        if (hasDeletedObjects()) {
            deletedObjects = new ArrayList(this.deletedObjects.size());
            for (Object objectToDelete : this.deletedObjects.keySet()) {
                ClassDescriptor descriptor = getDescriptor(objectToDelete);
                if (descriptor.hasPreDeleteMappings()) {
                    for (DatabaseMapping mapping : descriptor.getPreDeleteMappings()) {
                        DeleteObjectQuery deleteQuery = descriptor.getQueryManager().getDeleteQuery();
                        if (deleteQuery == null) {
                            deleteQuery = new DeleteObjectQuery();
                            deleteQuery.setDescriptor(descriptor);
                        } else {
                            // Ensure original query has been prepared.
                            deleteQuery.checkPrepare(this, deleteQuery.getTranslationRow());
                            deleteQuery = (DeleteObjectQuery) deleteQuery.clone();
                        }
                        deleteQuery.setIsExecutionClone(true);
                        deleteQuery.setTranslationRow(new DatabaseRecord());
                        deleteQuery.setObject(objectToDelete);
                        deleteQuery.setSession(this);
                        mapping.earlyPreDelete(deleteQuery, objectToDelete);
                    }
                }
                deletedObjects.add(objectToDelete);
            }
            this.preDeleteComplete = true;
        }
        if (this.shouldPerformDeletesFirst) {
            if (deletedObjects != null) {
                // This must go to the commit manager because uow overrides to do normal deletion.
                getCommitManager().deleteAllObjects(deletedObjects);
                // Clear change sets of the deleted object to avoid redundant updates.
                for (Iterator objects = getObjectsDeletedDuringCommit().keySet().iterator(); objects.hasNext(); ) {
                    org.eclipse.persistence.internal.sessions.ObjectChangeSet objectChangeSet = (org.eclipse.persistence.internal.sessions.ObjectChangeSet) this.unitOfWorkChangeSet.getObjectChangeSetForClone(objects.next());
                    if (objectChangeSet != null) {
                        objectChangeSet.clear(true);
                    }
                }
            }
            // Let the commit manager figure out how to write the objects
            super.writeAllObjectsWithChangeSet(this.unitOfWorkChangeSet);
            // Issue all the SQL for the ModifyAllQuery's, don't touch the cache though
            issueModifyAllQueryList();
        } else {
            // Let the commit manager figure out how to write the objects
            super.writeAllObjectsWithChangeSet(this.unitOfWorkChangeSet);
            if (deletedObjects != null) {
                // This must go to the commit manager because uow overrides to do normal deletion.
                getCommitManager().deleteAllObjects(deletedObjects);
            }
            // Issue all the SQL for the ModifyAllQuery's, don't touch the cache though
            issueModifyAllQueryList();
        }
        // Issue prepare event.
        if (this.eventManager != null) {
            this.eventManager.prepareUnitOfWork();
        }
        // do not lock objects unless we are at the commit stage
        if (commitTransaction) {
            try {
                acquireWriteLocks();
                commitTransaction();
            } catch (RuntimeException throwable) {
                releaseWriteLocks();
                throw throwable;
            } catch (Error throwable) {
                releaseWriteLocks();
                throw throwable;
            }
        } else {
            setWasTransactionBegunPrematurely(true);
        // must let the UnitOfWork know that the transaction was begun
        // before the commit process.
        }
    } catch (RuntimeException exception) {
        // The number of SQL statements been prepared need be stored into UOW
        // before any exception being thrown.
        copyStatementsCountIntoProperties();
        try {
            rollbackTransaction(commitTransaction);
        } catch (RuntimeException ignore) {
        // Ignore
        }
        if (hasExceptionHandler()) {
            getExceptionHandler().handleException(exception);
        } else {
            throw exception;
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ArrayList(java.util.ArrayList) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Iterator(java.util.Iterator) DescriptorIterator(org.eclipse.persistence.internal.descriptors.DescriptorIterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with DeleteObjectQuery

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

the class EISOneToManyMapping method preDelete.

/**
 * INTERNAL:
 * Delete the reference objects.
 */
@Override
public void preDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (isForeignKeyRelationship()) {
        return;
    }
    if (!this.shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    Object objects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
    ContainerPolicy cp = this.getContainerPolicy();
    // else delete everything in one shot
    if (this.mustDeleteReferenceObjectsOneByOne()) {
        for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
            DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
            deleteQuery.setIsExecutionClone(true);
            deleteQuery.setObject(cp.next(iter, query.getSession()));
            deleteQuery.setCascadePolicy(query.getCascadePolicy());
            query.getSession().executeQuery(deleteQuery);
        }
        if (!query.getSession().isUnitOfWork()) {
            // This deletes any objects on the database, as the collection in memory may have been changed.
            // This is not required for unit of work, as the update would have already deleted these objects,
            // and the backup copy will include the same objects causing double deletes.
            this.deleteReferenceObjectsLeftOnDatabase(query);
        }
    } else {
        this.deleteAll(query);
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery)

Example 4 with DeleteObjectQuery

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

the class EISOneToManyMapping method postDelete.

/**
 * INTERNAL:
 * Delete the reference objects.
 */
@Override
public void postDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!isForeignKeyRelationship()) {
        return;
    }
    if (!this.shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    Object referenceObjects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
    // otherwise, delete the reference objects one by one
    if (this.hasCustomDeleteAllQuery()) {
        this.deleteAll(query, referenceObjects);
    } else {
        ContainerPolicy cp = this.getContainerPolicy();
        for (Object iter = cp.iteratorFor(referenceObjects); cp.hasNext(iter); ) {
            DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
            deleteQuery.setIsExecutionClone(true);
            deleteQuery.setObject(cp.next(iter, query.getSession()));
            deleteQuery.setCascadePolicy(query.getCascadePolicy());
            query.getSession().executeQuery(deleteQuery);
        }
        if (!query.getSession().isUnitOfWork()) {
            // This deletes any objects on the database, as the collection in memory may have been changed.
            // This is not required for unit of work, as the update would have already deleted these objects,
            // and the backup copy will include the same objects, causing double deletes.
            this.deleteReferenceObjectsLeftOnDatabase(query);
        }
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery)

Example 5 with DeleteObjectQuery

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

the class CommitManager method deleteAllObjects.

/**
 * Delete all of the objects with the matching class.
 */
public void deleteAllObjects(Class<?> theClass, List objects, AbstractSession session) {
    ClassDescriptor descriptor = null;
    if (((UnitOfWorkImpl) session).getCommitOrder() != CommitOrderType.NONE) {
        // bug 331064 - Sort the delete order
        objects = sort(theClass, objects);
    }
    int size = objects.size();
    for (int index = 0; index < size; index++) {
        Object objectToDelete = objects.get(index);
        if (objectToDelete.getClass() == theClass) {
            if (descriptor == null) {
                descriptor = session.getDescriptor(theClass);
            }
            // PERF: Get the descriptor query, to avoid extra query creation.
            DeleteObjectQuery deleteQuery = descriptor.getQueryManager().getDeleteQuery();
            if (deleteQuery == null) {
                deleteQuery = new DeleteObjectQuery();
                deleteQuery.setDescriptor(descriptor);
            } else {
                // Ensure original query has been prepared.
                deleteQuery.checkPrepare(session, deleteQuery.getTranslationRow());
                deleteQuery = (DeleteObjectQuery) deleteQuery.clone();
            }
            deleteQuery.setIsExecutionClone(true);
            deleteQuery.setObject(objectToDelete);
            session.executeQuery(deleteQuery);
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery)

Aggregations

DeleteObjectQuery (org.eclipse.persistence.queries.DeleteObjectQuery)20 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)6 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)5 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DescriptorIterator (org.eclipse.persistence.internal.descriptors.DescriptorIterator)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)2 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 Vector (java.util.Vector)1 Expression (org.eclipse.persistence.expressions.Expression)1 IndirectList (org.eclipse.persistence.indirection.IndirectList)1