Search in sources :

Example 26 with CriteriaQuery

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

the class AdvancedCriteriaQueryTestSuite method testIsMember.

public void testIsMember() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> emp = cq.from(Employee.class);
        cq.where(qb.isMember(qb.parameter(String.class, "1"), emp.<Collection<String>>get("responsibilities")));
        Query query = em.createQuery(cq);
        query.setParameter("1", "Sort files");
        List<Employee> result = query.getResultList();
        assertFalse("No Employees were returned", result.isEmpty());
        for (Employee e : result) {
            assertTrue("Employee Found without Responcibilities", e.getResponsibilities().contains("Sort files"));
        }
    } 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) Collection(java.util.Collection)

Example 27 with CriteriaQuery

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

the class AdvancedCriteriaQueryTestSuite method testInlineInParameter.

public void testInlineInParameter() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> emp = cq.from(Employee.class);
        cq.where(emp.get("address").<String>get("city").in(qb.parameter(String.class, "city")));
        Query query = em.createQuery(cq);
        query.setParameter("city", "Ottawa");
        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)

Example 28 with CriteriaQuery

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

the class AdvancedQueryTestSuite method testTupleQuery.

/**
 * Test that a cache hit will occur on a primary key query.
 */
public void testTupleQuery() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    QuerySQLTracker counter = null;
    try {
        // Load an employee into the cache.
        CriteriaBuilder qb = em.getCriteriaBuilder();
        Query query = em.createQuery(em.getCriteriaBuilder().createQuery(Employee.class));
        List result = query.getResultList();
        Employee employee = (Employee) result.get(0);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        CriteriaQuery<Tuple> cq = qb.createQuery(Tuple.class);
        Root<Employee> from = cq.from(Employee.class);
        cq.multiselect(from.get("id"), from.get("firstName"));
        cq.where(qb.and(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")), qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName"))));
        TypedQuery<Tuple> typedQuery = em.createQuery(cq);
        typedQuery.setParameter("id", employee.getId());
        typedQuery.setParameter("firstName", employee.getFirstName());
        Tuple queryResult = typedQuery.getSingleResult();
        assertTrue("Query Results do not match selection", queryResult.get(0).equals(employee.getId()) && queryResult.get(1).equals(employee.getFirstName()));
    } 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) Tuple(jakarta.persistence.Tuple)

Example 29 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 30 with CriteriaQuery

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

the class AdvancedQueryTestSuite method testQueryCacheOnlyCacheHitsOnSession.

/**
 * Test that a cache hit will occur on a query when the object is not in the unit of work/em.
 */
public void testQueryCacheOnlyCacheHitsOnSession() {
    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(result.size() - 1);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        rollbackTransaction(em);
        closeEntityManager(em);
        em = createEntityManager();
        beginTransaction(em);
        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.QUERY_TYPE, QueryType.ReadObject);
        query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheOnly);
        query.setParameter("id", employee.getId());
        if (query.getSingleResult() == null) {
            fail("Query did not check session cache.");
        }
        if (counter.getSqlStatements().size() > 0) {
            fail("Cache hit do not occur: " + counter.getSqlStatements());
        }
        rollbackTransaction(em);
        closeEntityManager(em);
        em = createEntityManager();
        beginTransaction(em);
        cq = qb.createQuery(Employee.class);
        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.CheckCacheOnly);
        query.setParameter("id", employee.getId());
        if (query.getResultList().size() != 1) {
            fail("Query did not check session cache.");
        }
        if (counter.getSqlStatements().size() > 0) {
            fail("Cache hit do not occur: " + counter.getSqlStatements());
        }
    } finally {
        if (counter != null) {
            counter.remove();
        }
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
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)

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