Search in sources :

Example 16 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaSimpleTestSuiteBase method multipleExecutionOfCriteriaQueryTest.

public void multipleExecutionOfCriteriaQueryTest() {
    // bug 5279859
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        // "SELECT e FROM Employee e where e.address.postalCode = :postalCode"
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        cq.where(qb.equal(wrapper.get((Path) wrapper.get(wrapper.from(cq, Employee.class), Employee_address), Address_postalCode), qb.parameter(String.class, "postalCode")));
        Query query = em.createQuery(cq);
        query.setParameter("postalCode", "K1T3B9");
        try {
            query.getResultList();
        } catch (RuntimeException ex) {
            fail("Failed to execute query, exception resulted on first execution, not expected");
        }
        try {
            query.getResultList();
        } catch (RuntimeException ex) {
            fail("Failed to execute query, exception resulted on second execution");
        }
        query = em.createNamedQuery("findEmployeeByPostalCode", Employee.class);
        query.setParameter("postalCode", "K1T3B9");
        try {
            query.getResultList();
        } catch (RuntimeException ex) {
            fail("Failed to execute query, exception resulted on first execution, of second use of named query");
        }
        query.setMaxResults(100000);
        try {
            query.getResultList();
        } catch (RuntimeException ex) {
            fail("Failed to execute query, exception resulted after setting max results (forcing reprepare)");
        }
    } 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) Query(jakarta.persistence.Query) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) TypedQuery(jakarta.persistence.TypedQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery)

Example 17 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaUnitTestSuite method testMaxAndFirstResultsOnDataQueryWithGroupBy.

/**
 * Tests fix for bug6070214 that using Oracle Rownum pagination with group by
 * throws an SQl exception.
 */
public void testMaxAndFirstResultsOnDataQueryWithGroupBy() {
    Exception exception = null;
    List resultList = null;
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        clearCache();
        // "SELECT e.id FROM Employee e group by e.id"
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Integer> cq = qb.createQuery(Integer.class);
        Root<Employee> rootEmp = cq.from(Employee.class);
        Join<Object, Object> joinedManager = rootEmp.join("manager", JoinType.LEFT);
        cq.select(rootEmp.get("id"));
        Query query = em.createQuery(cq);
        try {
            query.setFirstResult(1);
            query.setMaxResults(1);
            resultList = query.getResultList();
        } catch (Exception e) {
            logThrowable(exception);
            exception = e;
        }
        Assert.assertNull("Exception was caught: " + exception, exception);
        Assert.assertTrue("Incorrect number of results returned.  Expected 1, returned " + resultList.size(), resultList.size() == 1);
    } 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) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaUnitTestSuite method testNonExistentConstructorCriteriaQuery.

public void testNonExistentConstructorCriteriaQuery() {
    Exception expectedException = null;
    EntityManager em = createEntityManager();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    // Test constructor object:
    CriteriaQuery<?> cq = qb.createQuery(DataHolder.class);
    Root<Employee> from = cq.from(Employee.class);
    EntityType<Employee> Emp_ = em.getMetamodel().entity(Employee.class);
    // IllegalArgumentException expected from multiselect since DataHolder(String) does not exist
    try {
        cq.multiselect(from.get(Emp_.getSingularAttribute("lastName", String.class)));
        Query query = em.createQuery(cq);
    } catch (IllegalArgumentException exception) {
        expectedException = exception;
    }
    assertNotNull("Expected IllegalArgumentException not thrown when using a non-existing constructor", expectedException);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery)

Example 19 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaUnitTestSuite method testIsNullAndIsNotNull.

public void testIsNullAndIsNotNull() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = (Employee) em.createQuery("select e from Employee e where e.firstName = 'John' and e.lastName = 'Way'").getSingleResult();
        emp.setFirstName(null);
        emp = (Employee) em.createQuery("select e from Employee e where e.firstName = 'Charles' and e.lastName = 'Chanley'").getSingleResult();
        emp.setFirstName(null);
        emp.setLastName(null);
        em.flush();
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<?> cq = qb.createQuery(Employee.class);
        Root<Employee> from = cq.from(Employee.class);
        EntityType<Employee> Emp_ = em.getMetamodel().entity(Employee.class);
        List<Predicate> criteriaList = new ArrayList<Predicate>();
        criteriaList.add(qb.isNotNull(from.get(Emp_.getSingularAttribute("lastName", String.class))));
        criteriaList.add(qb.isNull(from.get(Emp_.getSingularAttribute("firstName", String.class))));
        Predicate criteria = qb.and(criteriaList.toArray(new Predicate[0]));
        cq.where(criteria);
        Query query = em.createQuery(cq);
        List results = query.getResultList();
        assertTrue(results.size() == 1);
        emp = (Employee) results.get(0);
        assertTrue(emp.getFirstName() == null);
        assertTrue(emp.getLastName() != null);
    } finally {
        rollbackTransaction(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Predicate(jakarta.persistence.criteria.Predicate)

Example 20 with CriteriaQuery

use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaUnitTestSuite method testMaxAndFirstResultsOnDataQuery.

/**
 * Tests fix for bug6070214 that using Oracle Rownum pagination with non-unique columns
 * throws an SQl exception.
 */
public void testMaxAndFirstResultsOnDataQuery() {
    Exception exception = null;
    List resultList = null;
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        clearCache();
        // "SELECT e.id, m.id FROM Employee e LEFT OUTER JOIN e.manager m"
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Tuple> cq = qb.createTupleQuery();
        Root<Employee> rootEmp = cq.from(Employee.class);
        Join<Object, Object> joinedManager = rootEmp.join("manager", JoinType.LEFT);
        cq.multiselect(rootEmp.get("id"), joinedManager.get("id"));
        Query query = em.createQuery(cq);
        try {
            query.setFirstResult(1);
            query.setMaxResults(1);
            resultList = query.getResultList();
        } catch (Exception e) {
            logThrowable(exception);
            exception = e;
        }
        Assert.assertNull("Exception was caught: " + exception, exception);
        Assert.assertTrue("Incorrect number of results returned.  Expected 1, returned " + resultList.size(), resultList.size() == 1);
    } 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) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) List(java.util.List) Tuple(jakarta.persistence.Tuple)

Aggregations

CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)96 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)82 EntityManager (jakarta.persistence.EntityManager)64 Query (jakarta.persistence.Query)63 List (java.util.List)54 TypedQuery (jakarta.persistence.TypedQuery)35 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)32 Test (org.junit.jupiter.api.Test)24 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)20 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)16 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 ReportQuery (org.eclipse.persistence.queries.ReportQuery)12 Query (org.hibernate.query.Query)12 QuerySQLTracker (org.eclipse.persistence.testing.framework.QuerySQLTracker)11 BasicEntity (org.hibernate.testing.orm.domain.gambit.BasicEntity)11 Root (jakarta.persistence.criteria.Root)8 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)8 DatabasePlatform (org.eclipse.persistence.platform.database.DatabasePlatform)7