Search in sources :

Example 41 with CriteriaQuery

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

the class AdvancedQueryTestSuite method testUntypedPath.

/**
 * Test that a cache hit will occur on a primary key query.
 */
public void testUntypedPath() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    QuerySQLTracker counter = null;
    try {
        // Load an employee into the cache.
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Query query = em.createQuery(cq);
        List result = query.getResultList();
        Employee employee = (Employee) result.get(0);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        CriteriaQuery<Employee> empCQ = qb.createQuery(Employee.class);
        Root<Employee> from = empCQ.from(Employee.class);
        Join<Employee, Address> join = from.join("address");
        empCQ.where(qb.greaterThanOrEqualTo(from.get("id").as(Integer.class), qb.literal(5)));
        query = em.createQuery(empCQ);
        query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheByExactPrimaryKey);
        query.setParameter("id", employee.getId());
        Employee queryResult = (Employee) query.getSingleResult();
        if (queryResult != employee) {
            fail("Employees are not equal: " + employee + ", " + queryResult);
        }
        if (counter.getSqlStatements().size() > 0) {
            fail("Cache hit do not occur: " + counter.getSqlStatements());
        }
    } finally {
        rollbackTransaction(em);
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) TypedQuery(jakarta.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) List(java.util.List) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 42 with CriteriaQuery

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

the class AdvancedQueryTestSuite method testQueryExactPrimaryKeyCacheHits.

/**
 * Test that a cache hit will occur on a primary key query.
 */
public void testQueryExactPrimaryKeyCacheHits() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    QuerySQLTracker counter = null;
    try {
        // Load an employee into the cache.
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Query query = em.createQuery(cq);
        List result = query.getResultList();
        Employee employee = (Employee) result.get(0);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        cq = qb.createQuery(Employee.class);
        Root<Employee> from = cq.from(Employee.class);
        cq.where(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")));
        query = em.createQuery(cq);
        query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheByExactPrimaryKey);
        query.setParameter("id", employee.getId());
        Employee queryResult = (Employee) query.getSingleResult();
        if (queryResult != employee) {
            fail("Employees are not equal: " + employee + ", " + queryResult);
        }
        if (counter.getSqlStatements().size() > 0) {
            fail("Cache hit do not occur: " + counter.getSqlStatements());
        }
    } finally {
        rollbackTransaction(em);
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) TypedQuery(jakarta.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 43 with CriteriaQuery

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

the class AdvancedCriteriaQueryTestSuite method testInCollectionEntity.

public void testInCollectionEntity() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> emp = cq.from(Employee.class);
        Root<PhoneNumber> phone = cq.from(PhoneNumber.class);
        cq.where(qb.and(qb.equal(phone.get("areaCode"), "613"), phone.in(emp.<Collection<?>>get("phoneNumbers"))));
        Query query = em.createQuery(cq);
        List<Employee> result = query.getResultList();
        assertFalse("No Employees were returned", result.isEmpty());
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Query(jakarta.persistence.Query) TypedQuery(jakarta.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)

Example 44 with CriteriaQuery

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

the class TestCriteriaBuilder method testCriteriaBuilder_NOTIN_ClauseLimit.

@Test
public void testCriteriaBuilder_NOTIN_ClauseLimit() throws Exception {
    EntityManager em = emf.createEntityManager();
    try {
        // "SELECT OBJECT(emp) FROM Employee emp WHERE emp.id IN :result"
        final CriteriaBuilder builder = em.getCriteriaBuilder();
        final CriteriaQuery<L1> query = builder.createQuery(L1.class);
        Root<L1> root = query.from(L1.class);
        query.where(root.get("name").in(builder.parameter(List.class, "parameterList")).not());
        Query q = em.createQuery(query);
        // Create a list longer than the limit
        int limit = getPlatform(emf).getINClauseLimit() + 10;
        List<String> parameterList = new ArrayList<String>();
        for (int p = 0; p < limit; p++) {
            parameterList.add("" + p);
        }
        q.setParameter("parameterList", parameterList);
        q.getResultList();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) L1(org.eclipse.persistence.jpa.test.criteria.model.L1) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 45 with CriteriaQuery

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

the class CriteriaQueryTestSuite method testOnClauseWithLeftJoin.

public void testOnClauseWithLeftJoin() {
    EntityManager em = createEntityManager();
    Query query = em.createQuery("Select e from Employee e left join e.address a on a.city = 'Ottawa' " + "where a.postalCode is not null");
    List baseResult = query.getResultList();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    Root<Employee> root = cq.from(Employee.class);
    Join<Object, Object> address = root.join("address", JoinType.LEFT);
    address.on(qb.equal(address.get("city"), "Ottawa"));
    cq.where(qb.isNotNull(address.get("postalCode")));
    List<Employee> testResult = em.createQuery(cq).getResultList();
    clearCache();
    closeEntityManager(em);
    if (baseResult.size() != testResult.size()) {
        fail("Criteria query using ON clause with a left join did not match JPQL results; " + baseResult.size() + " were expected, while criteria query returned " + testResult.size());
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa22.advanced.Employee) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List)

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