Search in sources :

Example 26 with UpdateAllQuery

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

the class UpdateAllQueryAdvancedJunitTest method testFirstNamePrefixBLAForSalary.

public void testFirstNamePrefixBLAForSalary() {
    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")));
        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 27 with UpdateAllQuery

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

the class UpdateAllQueryAdvancedJunitTest method testAssignExpressionToAddress.

public void testAssignExpressionToAddress() {
    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("manager").notNull();
        UpdateAllQuery updateQuery = new UpdateAllQuery(Employee.class, selectionExpression);
        updateQuery.addUpdate("address", builder.get("manager").get("address"));
        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 28 with UpdateAllQuery

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

the class UpdateAllQueryAdvancedJunitTest method testFirstNamePrefixBLAForAll.

public void testFirstNamePrefixBLAForAll() {
    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("firstName", Expression.fromLiteral("'BLA'", null).concat(builder.get("firstName")));
        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 29 with UpdateAllQuery

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

the class ReportQueryAdvancedJUnitTest method clear.

protected static 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);
        uow.executeQuery(new DeleteAllQuery(Employee.class));
    } else {
        Iterator<Employee> emps = uow.readAllObjects(Employee.class).iterator();
        while (emps.hasNext()) {
            Employee emp = emps.next();
            emp.setManager(null);
            emp.setAddress(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));
    uow.executeQuery(new DeleteAllQuery(Project.class));
    uow.commit();
    clearCache("fieldaccess");
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DeleteAllQuery(org.eclipse.persistence.queries.DeleteAllQuery) UpdateAllQuery(org.eclipse.persistence.queries.UpdateAllQuery)

Example 30 with UpdateAllQuery

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

the class JoinedAttributeAdvancedJunitTest method testTwoUnrelatedResultWithOneToOneJoinsWithExtraItem.

public void testTwoUnrelatedResultWithOneToOneJoinsWithExtraItem() {
    if (getServerSession("fieldaccess").getPlatform().isSymfoware()) {
        getServerSession("fieldaccess").logMessage("Test testTwoUnrelatedResultWithOneToOneJoinsWithExtraItem skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    ReadAllQuery raq = new ReadAllQuery(Employee.class);
    raq.setSelectionCriteria(raq.getExpressionBuilder().get("lastName").equal("Way").or(raq.getExpressionBuilder().get("lastName").equal("Jones")));
    Employee emp = (Employee) ((Vector) getDbSession().executeQuery(raq)).firstElement();
    emp.getAddress();
    raq = new ReadAllQuery(Address.class);
    raq.setSelectionCriteria(raq.getExpressionBuilder().get("city").like("%ttawa%"));
    Address addr = (Address) ((Vector) getDbSession().executeQuery(raq)).firstElement();
    addr.getEmployees();
    for (Iterator<Employee> iterator = addr.getEmployees().iterator(); iterator.hasNext(); ) {
        iterator.next().getAddress();
    }
    getDbSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    ReportQuery query = new ReportQuery();
    query.setShouldReturnWithoutReportQueryResult(true);
    query.setReferenceClass(Employee.class);
    ExpressionBuilder eb = new ExpressionBuilder(Address.class);
    query.setSelectionCriteria(query.getExpressionBuilder().get("id").equal(emp.getId()).and(eb.get("id").equal(addr.getId())));
    List list = new ArrayList();
    list.add(query.getExpressionBuilder().get("address"));
    query.addItem("employee", query.getExpressionBuilder(), list);
    query.addItem("employee_name", query.getExpressionBuilder().get("firstName"));
    list = new ArrayList();
    list.add(eb.anyOf("employees"));
    query.addItem("address", eb, list);
    Vector result = (Vector) getDbSession().executeQuery(query);
    UpdateAllQuery updall = new UpdateAllQuery(Employee.class);
    updall.addUpdate("address", null);
    updall.setSelectionCriteria(updall.getExpressionBuilder().get("id").equal(emp.getId()));
    UnitOfWork uow = getDbSession().acquireUnitOfWork();
    uow.executeQuery(updall);
    updall = new UpdateAllQuery(Employee.class);
    updall.addUpdate("address", null);
    updall.setSelectionCriteria(updall.getExpressionBuilder().get("address").get("id").equal(addr.getId()));
    uow.executeQuery(updall);
    uow.commit();
    Employee emp2 = (Employee) ((Object[]) result.firstElement())[0];
    Address addr2 = (Address) ((Object[]) result.firstElement())[2];
    try {
        assertTrue("Address were not joined correctly, emp.getAddress() = null", (emp2.getAddress() != null));
        assertTrue("Employees were not joined correctly, addr.employees.size = " + addr.getEmployees().size() + "addr2.employees.size = " + addr2.getEmployees().size(), (addr.getEmployees().size() == addr2.getEmployees().size()));
        if (!emp2.getFirstName().equals(((Object[]) result.firstElement())[1])) {
            fail("Failed to return employee name as an separate item");
        }
    } finally {
        testSetup();
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector) 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