public void test() { ReadAllQuery query = new ReadAllQuery(); query.setReferenceClass(Employee.class); setSelectionCriteria(query); ReadAllQuery controlQuery = (ReadAllQuery) query.clone(); Expression employees = query.getExpressionBuilder().anyOf("managedEmployees"); query.addJoinedAttribute(employees); Expression phones = employees.anyOf("phoneNumbers"); query.addJoinedAttribute(phones); String errorMsg = JoinedAttributeTestHelper.executeQueriesAndCompareResults( controlQuery, query, (AbstractSession) getSession()); if (errorMsg.length() > 0) { throw new TestErrorException(errorMsg); } }
protected String executeQueriesAndCompareResults( ObjectLevelReadQuery controlQuery, ObjectLevelReadQuery queryWithJoins) { return JoinedAttributeTestHelper.executeQueriesAndCompareResults( controlQuery, queryWithJoins, (AbstractSession) getDbSession()); }
public void test() { // clear cache getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); // create batch read query, set its selectionCriteria ReadAllQuery query = new ReadAllQuery(Employee.class); setSelectionCriteria(query); // before adding batch read attributes clone the query to create control query ReadAllQuery controlQuery = (ReadAllQuery) query.clone(); // add batch read attributes Expression managedEmployees = query.getExpressionBuilder().get("managedEmployees"); Expression managedEmployeesPhoneNumbers = managedEmployees.get("phoneNumbers"); query.addBatchReadAttribute(managedEmployeesPhoneNumbers); // execute the query List employees = (List) getSession().executeQuery(query); if (employees.isEmpty()) { throw new TestProblemException("No Employees were read"); } // need to instantiate only a single Phone on a single managed Employee to trigger sql that // reads data from the db for all. // still need to trigger all the indirections - but (except the first one) they are not // accessing the db // (the data is already cached in the value holders). printDebug("Trigger batch reading results"); boolean isConnected = true; for (int i = 0; i < employees.size(); i++) { Employee manager = (Employee) employees.get(i); if (!manager.getManagedEmployees().isEmpty()) { printDebug("Manager = " + manager); for (int j = 0; j < manager.getManagedEmployees().size(); j++) { Employee emp = (Employee) manager.getManagedEmployees().get(j); printDebug(" " + emp); for (int k = 0; k < emp.getPhoneNumbers().size(); k++) { if (isConnected) { // need to instantiate only a single Phone on a single managed Employee to trigger // sql that reads data from the db for all. // to ensure that no other sql is issued close connection. ((AbstractSession) getSession()).getAccessor().closeConnection(); isConnected = false; } PhoneNumber phone = (PhoneNumber) emp.getPhoneNumbers().get(k); printDebug(" " + phone); } } } else { printDebug(manager.toString()); } } if (!isConnected) { // reconnect connection ((AbstractSession) getSession()) .getAccessor() .reestablishConnection((AbstractSession) getSession()); } printDebug(""); // obtain control results // clear cache getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); // execute control query List controlEmployees = (List) getSession().executeQuery(controlQuery); // instantiate all value holders that the batch query expected to instantiate printDebug("Trigger control results"); for (int i = 0; i < controlEmployees.size(); i++) { Employee manager = (Employee) controlEmployees.get(i); if (!manager.getManagedEmployees().isEmpty()) { printDebug("Manager = " + manager); for (int j = 0; j < manager.getManagedEmployees().size(); j++) { Employee emp = (Employee) manager.getManagedEmployees().get(j); printDebug(" " + emp); for (int k = 0; k < emp.getPhoneNumbers().size(); k++) { PhoneNumber phone = (PhoneNumber) emp.getPhoneNumbers().get(k); printDebug(" " + phone); } } } else { printDebug(manager.toString()); } } // compare results String errorMsg = JoinedAttributeTestHelper.compareCollections( employees, controlEmployees, getSession().getClassDescriptor(Employee.class), ((AbstractSession) getSession())); if (errorMsg.length() > 0) { throw new TestErrorException(errorMsg); } }