public void findAllEmployeesWithPhoneNumbers() { EntityManager em = createEntityManager(); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = builder.isEmpty("phoneNumbers").not(); ReadAllQuery raq = new ReadAllQuery(Employee.class); raq.setSelectionCriteria(whereClause); raq.useDistinct(); List expectedResult = (List) getServerSession().executeQuery(raq); String ejbqlString = "SELECT DISTINCT e FROM Employee e, IN (e.phoneNumbers) l"; Query query = em.createQuery(ejbqlString); if (usesSOP() && getServerSession().getPlatform().isOracle()) { // distinct is incompatible with blob in selection clause on Oracle query.setHint(QueryHints.SERIALIZED_OBJECT, "false"); } List firstResult = query.getResultList(); String alternateEjbqlString = "SELECT e FROM Employee e WHERE e.phoneNumbers IS NOT EMPTY"; List secondResult = em.createQuery(alternateEjbqlString).getResultList(); // 14 employees returned Assert.assertEquals( "Ejbql statements returned different results: data validation error", firstResult.size(), 14); Assert.assertTrue( "Equivalent Ejbql statements returned different results", comparer.compareObjects(secondResult, firstResult)); Assert.assertTrue( "Find all employees with phone numbers test failed", comparer.compareObjects(expectedResult, firstResult)); }
public void buildJoinSubclassesQuery(DescriptorQueryManager queryManager) { ReadAllQuery namedQuery = new ReadAllQuery(org.eclipse.persistence.testing.models.employee.domain.Project.class); namedQuery.useCursoredStream(); namedQuery.setShouldOuterJoinSubclasses(true); queryManager.addQuery("joinSubclassesQuery", namedQuery); }
protected void buildExpectedResults() { ReadAllQuery query = new ReadAllQuery(); query.setReferenceClass(Employee.class); Vector employees = (Vector) getSession().executeQuery(query); Vector distinctEmployees = new Vector(); // initialize distinctEmployees distinctEmployees.addElement(employees.elementAt(0)); // check employees with duplicate province and add only distinct employees to distinctEmployees for (int i = 1; i < employees.size(); i++) { boolean duplicateFound = false; // iterate through distinctEmployees to check for duplicate provinces, if found, employee not // added for (int j = 0; j < distinctEmployees.size(); j++) { if ((((Employee) employees.elementAt(i)).getAddress().getProvince()) .equals((((Employee) distinctEmployees.elementAt(j)).getAddress().getProvince()))) { duplicateFound = true; } } if (!duplicateFound) { distinctEmployees.addElement(employees.elementAt(i)); } } for (Enumeration e = distinctEmployees.elements(); e.hasMoreElements(); ) { Employee emp = (Employee) e.nextElement(); Object[] result = new Object[1]; result[0] = emp.getAddress().getProvince(); addResult(result, null); } }
public void setup() { Employee emp = (Employee) getSomeEmployees().firstElement(); String partOne; String partTwo; String ejbqlString; partOne = emp.getFirstName(); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = builder.get("firstName").concat("Smith").like(partOne + "Smith"); ReadAllQuery raq = new ReadAllQuery(); raq.setReferenceClass(Employee.class); raq.setSelectionCriteria(whereClause); Vector employees = (Vector) getSession().executeQuery(raq); ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE "; ejbqlString = ejbqlString + "CONCAT(emp.firstName,\"Smith\") LIKE "; ejbqlString = ejbqlString + "\"" + partOne + "Smith\""; setEjbqlString(ejbqlString); setOriginalOject(employees); super.setup(); }
public static DatabaseQuery buildPersistenceTestGetAllowingNullEqualQuery() { ExpressionBuilder builder = new ExpressionBuilder(Employee.class); Expression expression = builder.getAllowingNull("address").get("city").equal("Toronto"); ReadAllQuery query = new ReadAllQuery(Employee.class); query.setSelectionCriteria(expression); return query; }
public void testDeleteExpression() { if (isOnServer()) { // Not work on server. return; } if ((JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) { getServerSession() .logMessage( "Test testDeleteExpression skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193)."); return; } JpaEntityManager em = (org.eclipse.persistence.jpa.JpaEntityManager) createEntityManager(); try { beginTransaction(em); String orderString = "DELETE FROM OrderBean o WHERE o.customer.name ='Karen McDonald' "; em.createQuery(orderString).executeUpdate(); orderString = "DELETE FROM OrderBean o WHERE o.billedCustomer.name ='Karen McDonald' "; em.createQuery(orderString).executeUpdate(); String ejbqlString = "DELETE FROM Customer c WHERE c.name='Karen McDonald' "; int result = em.createQuery(ejbqlString).executeUpdate(); Assert.assertEquals("Delete Expression test failed: customer to delete not found", 1, result); em.flush(); ReadAllQuery raq = new ReadAllQuery(Customer.class, new ExpressionBuilder()); Expression whereClause = raq.getExpressionBuilder().get("name").equal("Karen McDonald"); raq.setSelectionCriteria(whereClause); List customerFound = (List) em.getActiveSession().executeQuery(raq); Assert.assertEquals("Delete Expression test failed", 0, customerFound.size()); } finally { rollbackTransaction(em); } }
public void getOrderLargerThan() { EntityManager em = createEntityManager(); ExpressionBuilder builder1 = new ExpressionBuilder(Order.class); ExpressionBuilder builder2 = new ExpressionBuilder(Order.class); Expression o1Quantity = builder1.get("quantity"); Expression o2Quantity = builder2.get("quantity"); Expression quantityComparison = o1Quantity.greaterThan(o2Quantity); Expression o2CustomerName = builder2.get("customer").get("name"); Expression nameComparison = o2CustomerName.equal("Jane Smith"); Expression whereClause = quantityComparison.and(nameComparison); ReadAllQuery raq = new ReadAllQuery(); raq.setSelectionCriteria(whereClause); raq.setReferenceClass(Order.class); raq.useDistinct(); List expectedResult = (List) getServerSession().executeQuery(raq); String ejbqlString = "SELECT DISTINCT o1 FROM OrderBean o1, OrderBean o2 WHERE o1.quantity > o2.quantity AND" + " o2.customer.name = 'Jane Smith' "; List result = em.createQuery(ejbqlString).getResultList(); // only 1 order Assert.assertEquals( "Get order larger than test failed: data validation error", result.size(), 1); Assert.assertTrue( "Get order larger than test failed", comparer.compareObjects(expectedResult, result)); }
public void getOrderForCustomer() { EntityManager em = createEntityManager(); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = builder.get("name").equal("Jane Smith"); ReadAllQuery raq = new ReadAllQuery(Customer.class); raq.setSelectionCriteria(whereClause); Customer expectedCustomer = (Customer) (((List) getServerSession().executeQuery(raq)).get(0)); SalesPerson salesPerson = ((Order) (expectedCustomer.getOrders().iterator().next())).getSalesPerson(); String ejbqlString = "SELECT DISTINCT c FROM Customer c JOIN c.orders o JOIN o.salesPerson s WHERE s.id = " + salesPerson.getId(); List firstResult = em.createQuery(ejbqlString).getResultList(); String alternateEjbqlString = "SELECT DISTINCT c FROM Customer c, IN(c.orders) o WHERE o.salesPerson.id = " + salesPerson.getId(); List secondResuslt = em.createQuery(alternateEjbqlString).getResultList(); // only 1 order for this customer Assert.assertEquals( "Get order for customer test failed: data validation error", firstResult.size(), 1); Assert.assertTrue( "Get order for customer test failed: two equivalent ejb queries return different results", comparer.compareObjects(secondResuslt, firstResult)); Assert.assertTrue( "Get order for customer test failed", comparer.compareObjects(expectedCustomer, firstResult)); }
public void setup() { Vector employees = getSomeEmployees(); // Bug 223005: Verify that we have at least 1 employee with the required field length otherwise // an EclipseLinkException will be thrown Employee emp = getEmployeeWithRequiredNameLength(employees, MIN_FIRSTNAME_LENGTH, getName()); String partialFirstName = "%" + emp.getFirstName().substring(0, 3) + "%"; ReadAllQuery raq = new ReadAllQuery(); raq.setReferenceClass(Employee.class); Vector parameters = new Vector(); parameters.add(partialFirstName); ExpressionBuilder eb = new ExpressionBuilder(); Expression whereClause = eb.get("firstName").like(partialFirstName); raq.setSelectionCriteria(whereClause); employees = (Vector) getSession().executeQuery(raq); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName LIKE ?1"; setEjbqlString(ejbqlString); setOriginalOject(employees); setArguments(parameters); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); setArgumentNames(myArgumentNames); super.setup(); }
public void findEmployeeWithWorkPhone2258812() { EntityManager em = createEntityManager(); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause1 = builder.anyOf("phoneNumbers").get("type").equal("Work"); Expression whereClause2 = builder.anyOf("phoneNumbers").get("number").equal("2258812"); ReadAllQuery raq = new ReadAllQuery(Employee.class); raq.setSelectionCriteria(whereClause1.and(whereClause2)); if (usesSOP() && getServerSession().getPlatform().isOracle()) { // distinct is incompatible with blob in selection clause on Oracle } else { raq.useDistinct(); } List expectedResult = (List) getServerSession().executeQuery(raq); String ejbqlString; if (usesSOP() && getServerSession().getPlatform().isOracle()) { // distinct is incompatible with blob in selection clause on Oracle ejbqlString = "SELECT e FROM Employee e JOIN e.phoneNumbers p " + "WHERE p.type = 'Work' AND p.number = '2258812' "; } else { ejbqlString = "SELECT DISTINCT e FROM Employee e JOIN e.phoneNumbers p " + "WHERE p.type = 'Work' AND p.number = '2258812' "; } List result = em.createQuery(ejbqlString).getResultList(); // 8 employees Assert.assertEquals( "Find employee with 2258812 number test failed: data validation error", result.size(), 8); Assert.assertTrue( "Find employee with 2258812 number test failed", comparer.compareObjects(expectedResult, result)); }
public static DatabaseQuery buildPersistenceTestAnyOfAllowingNoneEqualQuery() { ExpressionBuilder builder = new ExpressionBuilder(Employee.class); Expression expression = builder.anyOfAllowingNone("phoneNumbers").get("areaCode").equal("613"); ReadAllQuery query = new ReadAllQuery(Employee.class); query.setSelectionCriteria(expression); return query; }
public void setup() { Employee emp1; Employee emp2; Employee emp3; emp1 = (Employee) getSomeEmployees().firstElement(); emp2 = (Employee) getSomeEmployees().elementAt(1); emp3 = (Employee) getSomeEmployees().elementAt(2); ExpressionBuilder builder = new ExpressionBuilder(); Vector idVector = new Vector(); idVector.add(emp1.getId()); idVector.add(emp2.getId()); idVector.add(emp3.getId()); Expression whereClause = builder.get("id").notIn(idVector); ReadAllQuery raq = new ReadAllQuery(); raq.setReferenceClass(Employee.class); raq.setSelectionCriteria(whereClause); setOriginalOject(getSession().executeQuery(raq)); getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE emp.id NOT IN ("; ejbqlString = ejbqlString + emp1.getId().toString() + ", "; ejbqlString = ejbqlString + emp2.getId().toString() + ", "; ejbqlString = ejbqlString + emp3.getId().toString(); ejbqlString = ejbqlString + ")"; setEjbqlString(ejbqlString); super.setup(); }
public void run() { org.eclipse.persistence.queries.ReadAllQuery query = new org.eclipse.persistence.queries.ReadAllQuery(ConcurrentLargeProject.class); query.setSelectionCriteria(query.getExpressionBuilder().get("id").equal(idToUse)); query.addJoinedAttribute("location"); query.refreshIdentityMapResult(); session.executeQuery(query); }
/** INTERNAL: Clone the query. */ public Object clone() { ReadAllQuery cloneQuery = (ReadAllQuery) super.clone(); // Don't use setters as that will trigger unprepare cloneQuery.containerPolicy = getContainerPolicy().clone(cloneQuery); return cloneQuery; }
public void setup() { getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); allEmployees = new Vector(); queryAll = new ReadAllQuery(); queryAll.setReferenceClass(Employee.class); queryAll.setSelectionCriteria( new ExpressionBuilder().get("address").get("city").greaterThan("Montreal")); allEmployees = (Vector) getSession().executeQuery(queryAll); }
protected void setup() { getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); descriptorToModify = project.getDescriptors().get(Employee.class); ReadAllQuery testReadQuery = new ReadAllQuery(); SQLCall testCall = new SQLCall(); testReadQuery.setCall(testCall); // setting the SQLCall on ReadObjectQuery testReadQuery.setSQLString("testString"); descriptorToModify.getQueryManager().setReadAllQuery(testReadQuery); }
public void testEmployeeJoinProjects() { ReadAllQuery query = new ReadAllQuery(); query.setReferenceClass(Employee.class); ReadAllQuery controlQuery = (ReadAllQuery) query.clone(); query.addJoinedAttribute(query.getExpressionBuilder().anyOf("projects")); String errorMsg = executeQueriesAndCompareResults(controlQuery, query); if (errorMsg.length() > 0) { fail(errorMsg); } }
private void addOuterJoinJoiningComplexTest() { ExpressionBuilder emp = new ExpressionBuilder(); ReadAllExpressionTest test = new ReadAllOuterJoinExpressionTest(Project.class, 15); test.setName("OuterJoinJoiningComplexTest"); test.setDescription("Test joining with outer joins"); ReadAllQuery query = new ReadAllQuery(Project.class); query.addJoinedAttribute(emp.getAllowingNull("teamLeader")); test.setQuery(query); addTest(test); }
private void addOuterJoinJoiningTest() { ExpressionBuilder emp = new ExpressionBuilder(); ReadAllExpressionTest test = new ReadAllOuterJoinExpressionTest(Employee.class, 12); test.setName("OuterJoinJoiningTest"); test.setDescription("Test joining with outer joins"); ReadAllQuery query = new ReadAllQuery(Employee.class); query.addJoinedAttribute(emp.getAllowingNull("address")); test.setQuery(query); addTest(test); }
private void addOuterJoinOrderByTest() { ExpressionBuilder emp = new ExpressionBuilder(); ReadAllExpressionTest test = new ReadAllOuterJoinExpressionTest(Employee.class, 12); test.setName("OuterJoinOrderByTest"); test.setDescription("Test order by with outer joins"); ReadAllQuery query = new ReadAllQuery(Employee.class); query.addOrdering(emp.getAllowingNull("address").get("city")); test.setQuery(query); addTest(test); }
protected void test() { query = new ReadAllQuery(); query.setReferenceClass(referenceClass); ReportQuery hierarchyQuery = new ReportQuery(); hierarchyQuery.setReferenceClass(referenceClass); // The #employeeId is what would cause the error hierarchyQuery.setCall(new SQLCall("SELECT EMP_ID FROM EMPLOYEE WHERE MANAGER_ID=#employeeId")); query.setSelectionCriteria(query.getExpressionBuilder().get("id").in(hierarchyQuery)); // want the argument at the top level query query.addArgument("employeeId"); }
public List<Reservation> retrieveAll() { em = factory.createEntityManager(); em.getTransaction().begin(); ReadAllQuery query = new ReadAllQuery(); Reservation obj = new Reservation(); query.setExampleObject(obj); JpaEntityManager jpa = (JpaEntityManager) em.getDelegate(); List<Reservation> results = (List<Reservation>) jpa.getServerSession().acquireClientSession().executeQuery(query); return results; }
/** * INTERNAL: Return if the query is equal to the other. This is used to allow dynamic expression * query SQL to be cached. */ public boolean equals(Object object) { if (this == object) { return true; } if (!super.equals(object)) { return false; } ReadAllQuery query = (ReadAllQuery) object; if (!getContainerPolicy().equals(query.getContainerPolicy())) { return false; } return true; }
public void testReadAll() throws Exception { String sql = "select GID, GEOMETRY from SIMPLE_SPATIAL ORDER BY GID"; SQLReader reader = new SQLReader(session, sql); ReadAllQuery raq = new ReadAllQuery(SimpleSpatial.class); raq.addAscendingOrdering("id"); raq.setHintString("/*+ ORDERED */ "); List<Spatial> results = (List<Spatial>) session.executeQuery(raq); String compareResult = reader.compare(results); assertNull(compareResult, compareResult); }
private void addOuterJoinJoiningTest2() { ExpressionBuilder emp = new ExpressionBuilder(); ReadAllExpressionTest test = new ReadAllOuterJoinExpressionTest2( org.eclipse.persistence.testing.models.insurance.PolicyHolder.class, 4); test.setName("OuterJoinJoiningTest2"); test.setDescription("Test joining with outer joins"); ReadAllQuery query = new ReadAllQuery(org.eclipse.persistence.testing.models.insurance.PolicyHolder.class); query.addJoinedAttribute(emp.getAllowingNull("address")); test.setQuery(query); addTest(test); }
public void test() { ReadAllQuery query = new ReadAllQuery(Employee.class); query.dontMaintainCache(); query.addPartialAttribute("nonExistant"); try { getSession().executeQuery(query); } catch (QueryException exception) { if (exception.getErrorCode() == QueryException.SPECIFIED_PARTIAL_ATTRIBUTE_DOES_NOT_EXIST) { correctException = true; } else { correctException = false; throw exception; } } }
public void setup() { if (getSession().getLogin().getPlatform().isSQLServer() || getSession().getLogin().getPlatform().isSybase()) { throw new TestWarningException( "This test is not supported on SQL Server and Sybase. Because 'MOD' is not a recognized function name on SQL Server and Sybase."); } ReadAllQuery raq = new ReadAllQuery(); raq.setReferenceClass(getReferenceClass()); raq.setSelectionCriteria(getOriginalObjectExpression()); setOriginalOject(getSession().executeQuery(raq)); super.setup(); }
public static TestSuite getReadAllTestSuite() { TestSuite suite = new TestSuite(); suite.setName("InterfaceWithoutTablesReadAllTestSuite"); suite.setDescription( "This suite tests the reading of all the objects of each class in the interface model (without tables)."); ReadAllQuery query = new ReadAllQuery(); query.setReferenceClass(Unionized.class); query.useCollectionClass(java.util.ArrayList.class); ReadAllTest aTest = new ReadAllTest(Unionized.class, 6); aTest.setQuery(query); suite.addTest(aTest); suite.addTest(new ReadAllTest(Actor.class, 4)); suite.addTest(new ReadAllTest(Documentary.class, 1)); suite.addTest(new ReadAllTest(Film.class, 3)); suite.addTest(new ReadAllTest(Job.class, 18)); suite.addTest(new ReadAllTest(ManagerialJob.class, 6)); suite.addTest(new ReadAllTest(VIP.class, 3)); // Used to test Cursored Streams ReadAllTest test = new ReadAllTest(VIP.class, 3); test.setQuery(new ReadAllQuery()); test.getQuery().setReferenceClass(VIP.class); test.getQuery().useCursoredStream(); suite.addTest(test); // Test the non-availability of batch reading test = new ReadAllBatchTest(Employee.class, 3); test.setName("Batch Read Test"); test.setQuery(new ReadAllQuery()); test.getQuery().setReferenceClass(Employee.class); test.getQuery().addBatchReadAttribute("contact"); suite.addTest(test); suite.addTest(new OneToManyVariableBackBatchReadingTest()); // Test cascading test = new ReadAllTest(Employee.class, 4); test.setQuery(new ReadAllQuery()); test.getQuery().setReferenceClass(Employee.class); test.getQuery().cascadeAllParts(); suite.addTest(test); suite.addTest(new ReadAllTest(Employee.class, 4)); suite.addTest(new ReadAllConformInUowTest()); return suite; }
public void findAllEmployeesWithCellPhones() { EntityManager em = createEntityManager(); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = builder.anyOf("phoneNumbers").get("type").equal("Cellular"); ReadAllQuery raq = new ReadAllQuery(Employee.class); raq.setSelectionCriteria(whereClause); if (usesSOP() && getServerSession().getPlatform().isOracle()) { // distinct is incompatible with blob in selection clause on Oracle } else { raq.useDistinct(); } List expectedResult = (List) getServerSession().executeQuery(raq); String ejbqlString; if (usesSOP() && getServerSession().getPlatform().isOracle()) { // distinct is incompatible with blob in selection clause on Oracle ejbqlString = "SELECT e FROM Employee e JOIN e.phoneNumbers p " + "WHERE p.type = 'Cellular'"; } else { ejbqlString = "SELECT DISTINCT e FROM Employee e JOIN e.phoneNumbers p " + "WHERE p.type = 'Cellular'"; } List firstResult = em.createQuery(ejbqlString).getResultList(); String alternateEjbqlString; if (usesSOP() && getServerSession().getPlatform().isOracle()) { // distinct is incompatible with blob in selection clause on Oracle alternateEjbqlString = "SELECT e FROM Employee e INNER JOIN e.phoneNumbers p " + "WHERE p.type = 'Cellular'"; } else { alternateEjbqlString = "SELECT DISTINCT e FROM Employee e INNER JOIN e.phoneNumbers p " + "WHERE p.type = 'Cellular'"; } List secondResult = em.createQuery(alternateEjbqlString).getResultList(); // 4 employees returned Assert.assertEquals( "Find all employees with cellular phone numbers test failed: data validation error", firstResult.size(), 4); Assert.assertTrue( "Find all employees with cellular phone numbers test failed: two equivalent ejb queries return different results", comparer.compareObjects(secondResult, firstResult)); Assert.assertTrue( "Find all employees with cellular phone numbers test failed", comparer.compareObjects(expectedResult, secondResult)); }
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); } }