Search in sources :

Example 1 with DeleteAllQuery

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

the class JPQLParseTree method populateQuery.

/**
 * Add all of the relevant query settings from an EJBQLParseTree to the given
 * database query.
 * @param query The query to populate
 * @param session The session to use to information such as descriptors.
 */
public void populateQuery(DatabaseQuery query, AbstractSession session) {
    if (query.isObjectLevelReadQuery()) {
        ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery) query;
        GenerationContext generationContext = buildContext(objectQuery, session);
        populateReadQueryInternal(objectQuery, generationContext);
    } else if (query.isUpdateAllQuery()) {
        UpdateAllQuery updateQuery = (UpdateAllQuery) query;
        GenerationContext generationContext = buildContext(updateQuery, session);
        populateModifyQueryInternal(updateQuery, generationContext);
        addUpdatesToQuery(updateQuery, generationContext);
    } else if (query.isDeleteAllQuery()) {
        DeleteAllQuery deleteQuery = (DeleteAllQuery) query;
        GenerationContext generationContext = buildContext(deleteQuery, session);
        populateModifyQueryInternal(deleteQuery, generationContext);
    }
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) UpdateAllQuery(org.eclipse.persistence.queries.UpdateAllQuery)

Example 2 with DeleteAllQuery

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

the class WriteChanges_DeleteAll_TestCase method test.

@Override
public void test() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    try {
        Vector employees = uow.readAllObjects(Employee.class);
        uow.writeChanges();
        DeleteAllQuery deleteAllQuery = new DeleteAllQuery();
        deleteAllQuery.setReferenceClass(Employee.class);
        deleteAllQuery.setObjects(employees);
        uow.executeQuery(deleteAllQuery);
    } catch (Exception e) {
        exception = e;
    } finally {
        uow.release();
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) Vector(java.util.Vector) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) ValidationException(org.eclipse.persistence.exceptions.ValidationException)

Example 3 with DeleteAllQuery

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

the class STI_JoinedAttributeTest method clear.

protected void clear() {
    UnitOfWork uow = getSession().acquireUnitOfWork();
    UpdateAllQuery updateEmployees = new UpdateAllQuery(STI_Employee.class);
    updateEmployees.addUpdate("manager", null);
    uow.executeQuery(updateEmployees);
    UpdateAllQuery updateProjects = new UpdateAllQuery(STI_Project.class);
    updateProjects.addUpdate("teamLeader", null);
    uow.executeQuery(updateProjects);
    uow.executeQuery(new DeleteAllQuery(STI_Employee.class));
    uow.executeQuery(new DeleteAllQuery(STI_Project.class));
    uow.commit();
    clearCache();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) UpdateAllQuery(org.eclipse.persistence.queries.UpdateAllQuery)

Example 4 with DeleteAllQuery

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

the class DeleteAllQueryTestHelper method execute.

protected static String execute(Session mainSession, Class<?> referenceClass, Expression selectionExpression, boolean shouldDeferExecutionInUOW, boolean handleChildren, Class<?> rootClass) {
    String errorMsg = "";
    clearCache(mainSession);
    // first delete using the original TopLink approach - one by one.
    UnitOfWork uow = mainSession.acquireUnitOfWork();
    // mainSession could be a ServerSession
    AbstractSession session = uow.getParent();
    // Will need to bring the db back to its original state
    // so that comparison of the deletion result would be possible.
    session.beginTransaction();
    Vector objectsToDelete = uow.readAllObjects(referenceClass, selectionExpression);
    ClassDescriptor descriptor = mainSession.getClassDescriptor(referenceClass);
    uow.deleteAllObjects(objectsToDelete);
    mainSession.logMessage("***delete one by one");
    uow.commit();
    Vector objectsLeftAfterOriginalDeletion = session.readAllObjects(rootClass);
    session.rollbackTransaction();
    // now delete using DeleteAllQuery.
    clearCache(mainSession);
    // bring all objects into cache
    session.readAllObjects(rootClass);
    uow = mainSession.acquireUnitOfWork();
    // mainSession could be a ServerSession
    session = uow.getParent();
    // Will need to bring the db back to its original state
    // so that the in case thre are children descriptors
    // they would still have objects to work with.
    session.beginTransaction();
    DeleteAllQuery query = new DeleteAllQuery(referenceClass, selectionExpression);
    query.setShouldDeferExecutionInUOW(shouldDeferExecutionInUOW);
    uow.executeQuery(query);
    mainSession.logMessage("***DeleteAllQuery for class " + referenceClass.getName());
    uow.commit();
    // verify that cache invalidation worked correctly:
    // deleted objects should've disappeared, others remain
    String classErrorMsg = "";
    for (int i = 0; i < objectsToDelete.size(); i++) {
        Object deletedObject = session.readObject(objectsToDelete.elementAt(i));
        if (deletedObject != null) {
            classErrorMsg = classErrorMsg + "Deleted object " + deletedObject + " is stil in cache; ";
            break;
        }
    }
    for (int i = 0; i < objectsLeftAfterOriginalDeletion.size(); i++) {
        Object remainingObject = objectsLeftAfterOriginalDeletion.elementAt(i);
        Object remainingObjectRead = session.readObject(remainingObject);
        if (remainingObjectRead == null) {
            classErrorMsg = classErrorMsg + "Remaining object " + remainingObject + " is not in cache; ";
            break;
        }
    }
    // now let's verify that the objects were correctly deleted from the db
    clearCache(mainSession);
    // deleted objects should've disappeared, others remain
    for (int i = 0; i < objectsToDelete.size(); i++) {
        Object deletedObject = session.readObject(objectsToDelete.elementAt(i));
        if (deletedObject != null) {
            classErrorMsg = classErrorMsg + "Deleted object " + deletedObject + " is stil in db; ";
            break;
        }
    }
    for (int i = 0; i < objectsLeftAfterOriginalDeletion.size(); i++) {
        Object remainingObject = objectsLeftAfterOriginalDeletion.elementAt(i);
        Object remainingObjectRead = session.readObject(remainingObject);
        if (remainingObjectRead == null) {
            classErrorMsg = classErrorMsg + "Remaining object " + remainingObject + " is not in db; ";
            break;
        }
    }
    session.rollbackTransaction();
    if (classErrorMsg.length() > 0) {
        String className = referenceClass.getName();
        String shortClassName = className.substring(className.lastIndexOf('.') + 1);
        errorMsg = errorMsg + " " + shortClassName + ": " + classErrorMsg;
    }
    if (handleChildren) {
        if (descriptor.hasInheritance() && descriptor.getInheritancePolicy().hasChildren()) {
            Iterator<ClassDescriptor> it = descriptor.getInheritancePolicy().getChildDescriptors().iterator();
            while (it.hasNext()) {
                ClassDescriptor childDescriptor = it.next();
                Class<?> childReferenceClass = childDescriptor.getJavaClass();
                errorMsg += execute(mainSession, childReferenceClass, selectionExpression, shouldDeferExecutionInUOW, handleChildren, rootClass);
            }
        }
    }
    return errorMsg;
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Vector(java.util.Vector) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 5 with DeleteAllQuery

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

the class OneToManyMapping method setDeleteAllSQLString.

/**
 * PUBLIC:
 * Set the SQL string used by the mapping to delete the target objects.
 * This allows the developer to override the SQL
 * generated by TopLink with a custom SQL statement or procedure call.
 * The arguments are
 * translated from the fields of the source row, by replacing the field names
 * marked by '#' with the values for those fields at execution time.
 * A one-to-many mapping will only use this delete all optimization if the target objects
 * can be deleted in a single SQL call. This is possible when the target objects
 * are in a single table, do not using locking, do not contain other privately-owned
 * parts, do not read subclasses, etc.
 * <p>
 * Example: "delete from PHONE where OWNER_ID = #EMPLOYEE_ID"
 */
@Override
public void setDeleteAllSQLString(String sqlString) {
    DeleteAllQuery query = new DeleteAllQuery();
    query.setSQLString(sqlString);
    setCustomDeleteAllQuery(query);
}
Also used : DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery)

Aggregations

DeleteAllQuery (org.eclipse.persistence.queries.DeleteAllQuery)29 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)16 UpdateAllQuery (org.eclipse.persistence.queries.UpdateAllQuery)12 Vector (java.util.Vector)9 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)4 ReportQuery (org.eclipse.persistence.queries.ReportQuery)4 Expression (org.eclipse.persistence.expressions.Expression)3 Iterator (java.util.Iterator)2 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)2 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)2 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)2 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1