Search in sources :

Example 1 with PhoneNumber

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);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) ParameterExpression(jakarta.persistence.criteria.ParameterExpression) Expression(org.eclipse.persistence.expressions.Expression) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Vector(java.util.Vector)

Example 2 with PhoneNumber

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);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with PhoneNumber

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);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BigInteger(java.math.BigInteger) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ReportQuery(org.eclipse.persistence.queries.ReportQuery) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Example 4 with PhoneNumber

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);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Expression(org.eclipse.persistence.expressions.Expression) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 5 with PhoneNumber

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);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Tuple(jakarta.persistence.Tuple)

Aggregations

PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)103 EntityManager (jakarta.persistence.EntityManager)100 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)97 Query (jakarta.persistence.Query)52 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)49 Test (org.junit.Test)43 FetchGroup (org.eclipse.persistence.queries.FetchGroup)35 TypedQuery (jakarta.persistence.TypedQuery)25 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)19 List (java.util.List)19 ArrayList (java.util.ArrayList)17 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)17 ReportQuery (org.eclipse.persistence.queries.ReportQuery)15 Vector (java.util.Vector)14 Expression (org.eclipse.persistence.expressions.Expression)14 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)14 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)13 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)12 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)11 FetchGroupTracker (org.eclipse.persistence.queries.FetchGroupTracker)9