use of org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testSelectPhoneNumberAreaCodeWithEmployee.
public void testSelectPhoneNumberAreaCodeWithEmployee() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
ExpressionBuilder employees = new ExpressionBuilder();
Expression exp = employees.get("firstName").equal("Bob");
exp = exp.and(employees.get("lastName").equal("Smith"));
Employee emp = (Employee) getServerSession().readAllObjects(Employee.class, exp).firstElement();
PhoneNumber phone = (PhoneNumber) ((Vector) emp.getPhoneNumbers()).firstElement();
String areaCode = phone.getAreaCode();
String firstName = emp.getFirstName();
ExpressionBuilder employeeBuilder = new ExpressionBuilder();
Expression phones = employeeBuilder.anyOf("phoneNumbers");
Expression whereClause = phones.get("areaCode").equal(areaCode).and(phones.get("owner").get("firstName").equal(firstName));
ReportQuery rq = new ReportQuery();
rq.setSelectionCriteria(whereClause);
rq.addAttribute("areaCode", phones.get("areaCode"));
rq.setReferenceClass(Employee.class);
rq.dontMaintainCache();
Vector expectedResult = getAttributeFromAll("areaCode", (Vector) getServerSession().executeQuery(rq), Employee.class);
clearCache();
// "SELECT phone.areaCode FROM Employee employee, IN (employee.phoneNumbers) phone " +
// "WHERE phone.areaCode = \"" + areaCode + "\" AND phone.owner.firstName = \"" + firstName + "\"";
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = qb.createQuery(String.class);
Root<Employee> root = cq.from(Employee.class);
Join<Object, Object> joinedPhone = root.join("phoneNumbers");
cq.select(joinedPhone.get("areaCode"));
cq.where(qb.and(qb.equal(joinedPhone.get("areaCode"), "613")), qb.equal(joinedPhone.get("owner").get("firstName"), firstName));
List<String> result = em.createQuery(cq).getResultList();
Assert.assertTrue("SimpleSelectPhoneNumberAreaCodeWithEmployee test failed !", comparer.compareObjects(result, expectedResult));
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testExistWithJoin.
public void testExistWithJoin() {
EntityManager em = createEntityManager();
List expected = em.createQuery("SELECT c FROM Employee c WHERE NOT EXISTS (SELECT o1 FROM c.phoneNumbers o1)").getResultList();
beginTransaction(em);
try {
CriteriaBuilder qbuilder = em.getCriteriaBuilder();
CriteriaQuery<Employee> cquery = qbuilder.createQuery(Employee.class);
Root<Employee> customer = cquery.from(Employee.class);
cquery.select(customer);
Subquery<PhoneNumber> sq = cquery.subquery(PhoneNumber.class);
// correlate subquery root to root of main query:
Root<Employee> sqc = sq.correlate(customer);
Join<Employee, PhoneNumber> sqo = sqc.join("phoneNumbers");
sq.select(sqo);
cquery.where(qbuilder.not(qbuilder.exists(sq)));
List<Employee> result = em.createQuery(cquery).getResultList();
Assert.assertTrue("testExistWithJoin test failed !", comparer.compareObjects(result, expected));
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method simpleCountTest.
// Test case for concat function with constants in EJBQL
public void simpleCountTest() {
EntityManager em = createEntityManager();
ReportQuery query = new ReportQuery();
query.setReferenceClass(PhoneNumber.class);
// need to specify Long return type
query.addCount("COUNT", new ExpressionBuilder().get("owner").get("id"), Long.class);
query.returnSingleAttribute();
query.dontRetrievePrimaryKeys();
query.setName("selectPhoneNumbersAssociatedToEmployees");
Vector expectedResult = (Vector) getServerSession().executeQuery(query);
clearCache();
// "SELECT COUNT(phone.owner) FROM PhoneNumber phone";
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Long> cq = qb.createQuery(Long.class);
Root<PhoneNumber> root = cq.from(PhoneNumber.class);
cq.select(qb.count(root.get(getEntityManagerFactory().getMetamodel().entity(PhoneNumber.class).getSingularAttribute("owner", Employee.class)).get(getEntityManagerFactory().getMetamodel().entity(Employee.class).getSingularAttribute("id", Integer.class))));
// cq.select(qb.count(root.get(PhoneNumber_.owner).get(Employee_.id)));
beginTransaction(em);
try {
List<Long> result = em.createQuery(cq).getResultList();
System.out.println(" results are :" + result);
qb = em.getCriteriaBuilder();
cq = qb.createQuery(Long.class);
root = wrapper.from(cq, PhoneNumber.class);
cq.select(qb.count(wrapper.get((Path) wrapper.get(root, PhoneNumber_owner), Employee_id)));
result = em.createQuery(cq).getResultList();
assertTrue("Simple Count test failed", expectedResult.elementAt(0).equals(result.get(0)));
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method collectionMemberIdentifierEqualsTest.
public void collectionMemberIdentifierEqualsTest() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
ExpressionBuilder employees = new ExpressionBuilder();
Expression exp = employees.get("firstName").equal("Bob");
exp = exp.and(employees.get("lastName").equal("Smith"));
Employee expectedResult = (Employee) getServerSession().readAllObjects(Employee.class, exp).firstElement();
clearCache();
PhoneNumber phoneNumber = (PhoneNumber) ((Vector) expectedResult.getPhoneNumbers()).firstElement();
// "SELECT OBJECT(emp) FROM Employee emp, IN (emp.phoneNumbers) phone " + "WHERE phone = ?1"
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
Join<Employee, PhoneNumber> phones = wrapper.join(wrapper.from(cq, Employee.class), Employee_phoneNumbers);
cq.where(qb.equal(phones, qb.parameter(PhoneNumber.class, "1")));
List<Employee> result = em.createQuery(cq).setParameter("1", phoneNumber).getResultList();
assertTrue("CollectionMemberIdentifierEqualsTest failed", comparer.compareObjects(expectedResult, result));
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testInvertedSelectionCriteriaNullPK.
/**
* Prior to the fix for GF 2333, the query in this test would a Null PK exception
*/
public void testInvertedSelectionCriteriaNullPK() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
// "SELECT e, p FROM Employee e, PhoneNumber p WHERE p.id = e.id AND e.firstName = 'Bob'"
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = qb.createTupleQuery();
Root<Employee> rootEmp = cq.from(Employee.class);
Root<PhoneNumber> rootPhone = cq.from(PhoneNumber.class);
cq.multiselect(rootEmp, rootPhone);
cq.where(qb.and(qb.equal(rootPhone.get("id"), rootEmp.get("id")), qb.equal(rootEmp.get("firstName"), "Bob")));
em.createQuery(cq).getResultList();
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
Aggregations