Search in sources :

Example 21 with UpdateAllQuery

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

the class ReportQueryAdvancedJUnitTest method clear.

protected void clear() {
    UnitOfWork uow = acquireUnitOfWork();
    // use alternate way for Symfoware as it doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193)
    if (!(JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
        UpdateAllQuery updateEmployees = new UpdateAllQuery(Employee.class);
        updateEmployees.addUpdate("manager", null);
        updateEmployees.addUpdate("address", null);
        uow.executeQuery(updateEmployees);
    } else {
        Iterator<Employee> emps = uow.readAllObjects(Employee.class).iterator();
        while (emps.hasNext()) {
            Employee emp = emps.next();
            emp.setManager(null);
            emp.setAddress((String) null);
            uow.deleteObject(emp);
        }
        ;
    }
    UpdateAllQuery updateProjects = new UpdateAllQuery(Project.class);
    updateProjects.addUpdate("teamLeader", null);
    uow.executeQuery(updateProjects);
    uow.executeQuery(new DeleteAllQuery(PhoneNumber.class));
    uow.executeQuery(new DeleteAllQuery(Address.class));
    if (!(JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
        uow.executeQuery(new DeleteAllQuery(Employee.class));
    }
    uow.executeQuery(new DeleteAllQuery(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 22 with UpdateAllQuery

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

the class UpdateAllQueryAdvancedJunitTest method clear.

protected void clear() {
    UnitOfWork uow = acquireUnitOfWork();
    UpdateAllQuery updateEmployees = new UpdateAllQuery(Employee.class);
    updateEmployees.addUpdate("manager", null);
    updateEmployees.addUpdate("address", null);
    uow.executeQuery(updateEmployees);
    UpdateAllQuery updateProjects = new UpdateAllQuery(Project.class);
    updateProjects.addUpdate("teamLeader", null);
    uow.executeQuery(updateProjects);
    uow.executeQuery(new DeleteAllQuery(PhoneNumber.class));
    uow.executeQuery(new DeleteAllQuery(Address.class));
    uow.executeQuery(new DeleteAllQuery(Employee.class));
    uow.executeQuery(new DeleteAllQuery(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 23 with UpdateAllQuery

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

the class UpdateAllQueryAdvancedJunitTest method testDoubleSalaryForAll.

public void testDoubleSalaryForAll() {
    if (getServerSession("fieldaccess").getPlatform().isSymfoware()) {
        warning("UpdateAllQueryAdvancedJunitTest skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    EntityManager em = createEntityManager("fieldaccess");
    beginTransaction(em);
    try {
        ExpressionBuilder builder = new ExpressionBuilder();
        UpdateAllQuery updateQuery = new UpdateAllQuery(Employee.class);
        updateQuery.addUpdate("salary", ExpressionMath.multiply(builder.get("salary"), Integer.valueOf(2)));
        updateAllQueryInternal(updateQuery);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) UpdateAllQuery(org.eclipse.persistence.queries.UpdateAllQuery)

Example 24 with UpdateAllQuery

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

the class UpdateAllQueryAdvancedJunitTest method testFirstNamePrefixBLADoubleSalaryForSalary.

public void testFirstNamePrefixBLADoubleSalaryForSalary() {
    if (getServerSession("fieldaccess").getPlatform().isSymfoware()) {
        warning("UpdateAllQueryAdvancedJunitTest skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    EntityManager em = createEntityManager("fieldaccess");
    beginTransaction(em);
    try {
        ExpressionBuilder builder = new ExpressionBuilder();
        Expression selectionExpression = builder.get("salary").lessThan(20000);
        UpdateAllQuery updateQuery = new UpdateAllQuery(Employee.class, selectionExpression);
        updateQuery.addUpdate("firstName", Expression.fromLiteral("'BLA'", null).concat(builder.get("firstName")));
        updateQuery.addUpdate("salary", ExpressionMath.multiply(builder.get("salary"), Integer.valueOf(2)));
        updateAllQueryInternal(updateQuery);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Expression(org.eclipse.persistence.expressions.Expression) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) UpdateAllQuery(org.eclipse.persistence.queries.UpdateAllQuery)

Example 25 with UpdateAllQuery

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

the class UpdateAllQueryAdvancedJunitTest method testAssignObjectToAddress.

public void testAssignObjectToAddress() {
    if (getServerSession("fieldaccess").getPlatform().isSymfoware()) {
        warning("UpdateAllQueryAdvancedJunitTest skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    EntityManager em = createEntityManager("fieldaccess");
    if (isOnServer()) {
        warning("Warning: The test uses UnitOfWork directly, not JPA, so will not work correctly with JTA. For details, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=305040");
        return;
    }
    beginTransaction(em);
    try {
        Address address = new Address();
        address.setCountry("Canada");
        address.setProvince("Ontario");
        address.setCity("Ottawa");
        address.setStreet("O'Connor");
        UnitOfWork uow = acquireUnitOfWork();
        uow.registerNewObject(address);
        uow.commit();
        UpdateAllQuery updateQuery = new UpdateAllQuery(Employee.class);
        updateQuery.addUpdate("address", address);
        updateAllQueryInternal(updateQuery);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) EntityManager(jakarta.persistence.EntityManager) UpdateAllQuery(org.eclipse.persistence.queries.UpdateAllQuery)

Aggregations

UpdateAllQuery (org.eclipse.persistence.queries.UpdateAllQuery)57 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)36 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)22 Expression (org.eclipse.persistence.expressions.Expression)19 EntityManager (jakarta.persistence.EntityManager)13 DeleteAllQuery (org.eclipse.persistence.queries.DeleteAllQuery)12 ReportQuery (org.eclipse.persistence.queries.ReportQuery)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Vector (java.util.Vector)9 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)8 Iterator (java.util.Iterator)7 HashMap (java.util.HashMap)5 Map (java.util.Map)4 IdentityHashMap (java.util.IdentityHashMap)3 DataExpression (org.eclipse.persistence.internal.expressions.DataExpression)3 ExpressionIterator (org.eclipse.persistence.internal.expressions.ExpressionIterator)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)2 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)2