public void test() { Session session = getSession(); UnitOfWork uow = session.acquireUnitOfWork(); uow.setShouldNewObjectsBeCached(true); Weather weather = new Weather(); weather.setStormPattern("Something really bad"); weather.id = 0; ReadObjectQuery query = new ReadObjectQuery(weather); query.checkCacheOnly(); Weather weatherClone = (Weather) uow.registerObject(weather); weather = (Weather) uow.executeQuery(query); zeroFailed = weather == null; Weather weatherNeg = new Weather(); weatherNeg.setStormPattern("Something really bad below zero"); weatherNeg.id = -1; ReadObjectQuery queryNeg = new ReadObjectQuery(weatherNeg); queryNeg.checkCacheOnly(); Weather weatherNegClone = (Weather) uow.registerObject(weatherNeg); weatherNeg = (Weather) uow.executeQuery(queryNeg); negativeFailed = weatherNeg == null; if (keepSequencing) { uow.assignSequenceNumbers(); zeroOverridden = weatherClone.id != 0; negativeOverridden = weatherNegClone.id != -1; } }
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("fieldaccess")).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(); dbSessionClearCache(); }
public void test() { UnitOfWork uow = getSession().acquireUnitOfWork(); UnitOfWork nestedUow1 = uow.acquireUnitOfWork(); UnitOfWork nestedNestedUOW = nestedUow1.acquireUnitOfWork(); Employee employee = (Employee) new EmployeePopulator().basicEmployeeExample1(); employee.setId(new BigDecimal(15)); Employee nestedEmployee = (Employee) nestedNestedUOW.registerObject(employee); nestedNestedUOW.commit(); nestedUow1.commit(); nestedUow1 = uow.acquireUnitOfWork(); nestedNestedUOW = nestedUow1.acquireUnitOfWork(); ReadObjectQuery query = new ReadObjectQuery(); query.setReferenceClass(Employee.class); query.setSelectionCriteria( new org.eclipse.persistence.expressions.ExpressionBuilder() .get("id") .equal(new BigDecimal(15))); query.conformResultsInUnitOfWork(); nestedEmployee = (Employee) nestedNestedUOW.executeQuery(query); nestedNestedUOW.deleteObject(nestedEmployee); nestedNestedUOW.commit(); nestedUow1.commit(); if (!((UnitOfWorkImpl) uow).getNewObjectsCloneToOriginal().isEmpty()) { throw new TestErrorException("Failed to unregister the Object in the nested unit of work"); } }
public void test() { // Make a query an search Number greaterThan "00005" ExpressionBuilder phone = new ExpressionBuilder(); Expression exp = phone.get("areaCode").equal(613); ReadAllQuery query = new ReadAllQuery(PhoneNumber.class); query.setSelectionCriteria(exp); query.conformResultsInUnitOfWork(); // set Conforming query.addOrdering(phone.get("owner").get("id").descending()); query.addOrdering(phone.get("type").descending()); UnitOfWork uow = getSession().acquireUnitOfWork(); Vector v = (Vector) uow.executeQuery(query); Vector v2 = (Vector) uow.executeQuery(query); if (!v.equals(v2)) { throw new TestErrorException("Order not maintained when conforming:" + v + " != " + v2); } }
protected void test() { UnitOfWork uow = getSession().acquireUnitOfWork(); ReadObjectQuery query = new ReadObjectQuery(Employee.class); query.setLockMode(ObjectLevelReadQuery.LOCK_NOWAIT); Employee emp = (Employee) uow.executeQuery(query); emp.setSalary(emp.getSalary() + 1); uow.commit(); }
public void test() { ReadAllQuery q = new ReadAllQuery(); q.setBatchFetchType(batchType); q.setReferenceClass(Shipment.class); q.addBatchReadAttribute("employee"); q.setSelectionCriteria( q.getExpressionBuilder() .get("employee") .get("address") .equal(q.getExpressionBuilder().getParameter("ADDRESS"))); q.addArgument("ADDRESS"); Vector r = new Vector(); r.addElement("885 Meadowlands Dr."); UnitOfWork uow = getSession().acquireUnitOfWork(); v = (java.util.Vector) uow.executeQuery(q, r); }
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 iterator = addr.getEmployees().iterator(); iterator.hasNext(); ) { ((Employee) 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(); } }
public void testMultipleUnrelatedResultWithOneToManyJoins() { if (getServerSession("fieldaccess").getPlatform().isSymfoware()) { getServerSession("fieldaccess") .logMessage( "Test testMultipleUnrelatedResultWithOneToManyJoins 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().notEmpty("phoneNumbers")); Employee emp = (Employee) ((Vector) getDbSession().executeQuery(raq)).firstElement(); emp.getPhoneNumbers(); for (Iterator iterator = emp.getPhoneNumbers().iterator(); iterator.hasNext(); ) { ((PhoneNumber) iterator.next()).getOwner(); } 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 iterator = addr.getEmployees().iterator(); iterator.hasNext(); ) { Employee addrEmp = (Employee) iterator.next(); addrEmp.getAddress(); addrEmp .getPhoneNumbers() .size(); // as the report query will join in all phones to all emps, make sure we can // compare. } getDbSession().getIdentityMapAccessor().initializeAllIdentityMaps(); ReportQuery query = new ReportQuery(); query.setShouldReturnWithoutReportQueryResult(true); query.setReferenceClass(Address.class); ExpressionBuilder eb = new ExpressionBuilder(Employee.class); List list = new ArrayList(); list.add(eb.anyOf("phoneNumbers")); query.addItem("employee", eb, list); list = new ArrayList(); list.add(query.getExpressionBuilder().anyOf("employees")); query.addItem("address", query.getExpressionBuilder(), list); query.setSelectionCriteria(query.getExpressionBuilder().get("id").equal(addr.getId())); Vector result = (Vector) getDbSession().executeQuery(query); DeleteAllQuery deleteAll = new DeleteAllQuery(PhoneNumber.class); deleteAll.setSelectionCriteria( deleteAll.getExpressionBuilder().get("owner").get("id").equal(emp.getId())); UnitOfWork uow = getDbSession().acquireUnitOfWork(); uow.executeQuery(deleteAll); UpdateAllQuery 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(); try { Employee emp2 = null; Address addr2 = null; for (Iterator iterator = result.iterator(); iterator.hasNext(); ) { Object[] items = (Object[]) iterator.next(); emp2 = (Employee) items[0]; if (emp2.getId().equals(emp.getId())) { addr2 = (Address) items[1]; break; } } assertTrue( "PhoneNumbers were not joined correctly, emp.getPhoneNumbers().size = " + emp.getPhoneNumbers().size() + " emp2.getPhoneNumbers().size = " + emp2.getPhoneNumbers().size(), (emp.getPhoneNumbers().size() == emp2.getPhoneNumbers().size())); assertTrue( "Employees were not joined correctly, addr.employees.size = " + addr.getEmployees().size() + "addr2.employees.size = " + addr2.getEmployees().size(), (addr.getEmployees().size() == addr2.getEmployees().size())); } finally { testSetup(); } }
public void test() { result = nestedUnitOfWork.executeQuery(conformedQuery); unitOfWork.release(); }