Search in sources :

Example 6 with DeleteObjectQuery

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

the class ObjectReferenceMapping method postDelete.

/**
 * INTERNAL:
 * Delete privately owned parts
 */
@Override
public void postDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    // Deletion takes place only if it has privately owned parts and mapping is not read only.
    if (!shouldObjectModifyCascadeToParts(query)) {
        return;
    }
    Object object = query.getProperty(this);
    // The object is stored in the query by preDeleteForObjectUsing(...).
    if (isForeignKeyRelationship()) {
        if (object != null) {
            query.removeProperty(this);
            AbstractSession session = query.getSession();
            // CR 2811
            if (query.isCascadeOfAggregateDelete()) {
                session.getCommitManager().addObjectToDelete(object);
            } else {
                // PERF: Avoid query execution if already deleted.
                if (session.getCommitManager().isCommitCompletedInPostOrIgnore(object)) {
                    return;
                }
                if (this.isCascadeOnDeleteSetOnDatabase && !hasRelationTableMechanism() && session.isUnitOfWork()) {
                    ((UnitOfWorkImpl) session).getCascadeDeleteObjects().add(object);
                }
                DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
                deleteQuery.setIsExecutionClone(true);
                deleteQuery.setObject(object);
                deleteQuery.setCascadePolicy(query.getCascadePolicy());
                session.executeQuery(deleteQuery);
            }
        }
    }
}
Also used : DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 7 with DeleteObjectQuery

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

the class OneToManyMapping method preDelete.

/**
 * INTERNAL:
 * Delete the reference objects.
 */
@Override
public void preDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
    if (!shouldObjectModifyCascadeToParts(query)) {
        if (this.listOrderField != null) {
            updateTargetRowPreDeleteSource(query);
        }
        return;
    }
    AbstractSession session = query.getSession();
    // else delete everything in one shot.
    if (mustDeleteReferenceObjectsOneByOne()) {
        Object objects = getRealCollectionAttributeValueFromObject(query.getObject(), session);
        ContainerPolicy cp = getContainerPolicy();
        if (this.isCascadeOnDeleteSetOnDatabase && session.isUnitOfWork()) {
            for (Object iterator = cp.iteratorFor(objects); cp.hasNext(iterator); ) {
                Object wrappedObject = cp.nextEntry(iterator, session);
                Object object = cp.unwrapIteratorResult(wrappedObject);
                ((UnitOfWorkImpl) session).getCascadeDeleteObjects().add(object);
            }
        }
        int cascade = query.getCascadePolicy();
        for (Object iterator = cp.iteratorFor(objects); cp.hasNext(iterator); ) {
            Object wrappedObject = cp.nextEntry(iterator, session);
            Object object = cp.unwrapIteratorResult(wrappedObject);
            // PERF: Avoid query execution if already deleted.
            if (!session.getCommitManager().isCommitCompletedInPostOrIgnore(object) || this.containerPolicy.propagatesEventsToCollection()) {
                if (session.isUnitOfWork() && ((UnitOfWorkImpl) session).isObjectNew(object)) {
                    session.getCommitManager().markIgnoreCommit(object);
                } else {
                    DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
                    deleteQuery.setIsExecutionClone(true);
                    deleteQuery.setObject(object);
                    deleteQuery.setCascadePolicy(cascade);
                    session.executeQuery(deleteQuery);
                    this.containerPolicy.propogatePreDelete(deleteQuery, wrappedObject);
                }
            }
        }
        if (!session.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.
            deleteReferenceObjectsLeftOnDatabase(query);
        }
    } else {
        deleteAll(query, session);
    }
}
Also used : ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 8 with DeleteObjectQuery

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

the class DirectCollectionMapping method postUpdate.

/**
 * INTERNAL:
 * Update private owned part.
 */
@Override
public void postUpdate(WriteObjectQuery writeQuery) throws DatabaseException {
    if (isReadOnly()) {
        return;
    }
    if (writeQuery.getObjectChangeSet() != null) {
        if (this.listOrderField != null) {
            postUpdateWithChangeSetListOrder(writeQuery);
        } else {
            postUpdateWithChangeSet(writeQuery);
        }
        return;
    }
    // If objects are not instantiated that means they are not changed.
    if (!isAttributeValueInstantiatedOrChanged(writeQuery.getObject())) {
        return;
    }
    if (writeQuery.getSession().isUnitOfWork()) {
        if (compareObjects(writeQuery.getObject(), writeQuery.getBackupClone(), writeQuery.getSession())) {
            // Nothing has changed, no work required
            return;
        }
    }
    DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
    deleteQuery.setObject(writeQuery.getObject());
    deleteQuery.setSession(writeQuery.getSession());
    deleteQuery.setTranslationRow(writeQuery.getTranslationRow());
    if (writeQuery.shouldCascadeOnlyDependentParts()) {
        // Hey I might actually want to use an inner class here... ok array for now.
        Object[] event = new Object[3];
        event[0] = DeleteAll;
        event[1] = deleteQuery;
        writeQuery.getSession().getCommitManager().addDataModificationEvent(this, event);
    } else {
        preDelete(deleteQuery);
    }
    postInsert(writeQuery);
}
Also used : DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery)

Example 9 with DeleteObjectQuery

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

the class AggregateCollectionMapping method objectRemovedDuringUpdate.

/**
 * INTERNAL:
 * An object was removed to the collection during an update, delete it if private.
 */
@Override
protected void objectRemovedDuringUpdate(ObjectLevelModifyQuery query, Object objectDeleted, Map extraData) throws DatabaseException, OptimisticLockException {
    // Delete must not be done for uow or cascaded queries and we must cascade to cascade policy.
    DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
    deleteQuery.setIsExecutionClone(true);
    prepareModifyQueryForDelete(query, deleteQuery, objectDeleted, extraData);
    ContainerPolicy.copyMapDataToRow(extraData, deleteQuery.getTranslationRow());
    query.getSession().executeQuery(deleteQuery, deleteQuery.getTranslationRow());
    if (containerPolicy.shouldIncludeKeyInDeleteEvent()) {
        query.getSession().deleteObject(containerPolicy.keyFromEntry(objectDeleted));
    }
}
Also used : DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery)

Example 10 with DeleteObjectQuery

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

the class DescriptorQueryManager method setDeleteSQLString.

/**
 * ADVANCED:
 * Set the receiver's delete SQL string.
 * This allows the user to override the SQL generated by EclipseLink, with their own SQL or procedure call.
 * The arguments are translated from the fields of the source row,
 * through replacing the field names marked by '#' with the values for those fields.
 * Warning: Allowing an unverified SQL string to be passed into this
 * method makes your application vulnerable to SQL injection attacks.
 * <p>
 *    Example, "delete from EMPLOYEE where EMPLOYEE_ID = #EMPLOYEE_ID".
 */
public void setDeleteSQLString(String sqlString) {
    if (sqlString == null) {
        return;
    }
    DeleteObjectQuery query = new DeleteObjectQuery();
    query.setSQLString(sqlString);
    setDeleteQuery(query);
}
Also used : 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