Search in sources :

Example 31 with CriteriaQuery

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

the class CriteriaQueryMetamodelTestSuite method testMetamodelOnClauseOverCollection.

public void testMetamodelOnClauseOverCollection() {
    EntityManager em = createEntityManager();
    Query query = em.createQuery("Select e from Employee e join e.phoneNumbers p on p.areaCode = '613'");
    List baseResult = query.getResultList();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    EntityType<PhoneNumber> entityPhone_ = metamodel.entity(PhoneNumber.class);
    Root<Employee> root = cq.from(entityEmp_);
    Join phoneNumber = root.join(entityEmp_.getCollection("phoneNumbers"));
    phoneNumber.on(qb.equal(phoneNumber.get(entityPhone_.getSingularAttribute("areaCode")), "613"));
    List testResult = em.createQuery(cq).getResultList();
    clearCache();
    closeEntityManager(em);
    if (baseResult.size() != testResult.size()) {
        fail("Criteria query using ON clause 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) Employee(org.eclipse.persistence.testing.models.jpa22.advanced.Employee) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) PhoneNumber(org.eclipse.persistence.testing.models.jpa22.advanced.PhoneNumber) Join(jakarta.persistence.criteria.Join) List(java.util.List) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 32 with CriteriaQuery

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

the class JUnitCriteriaSimpleTestSuite method testOneEqualsOne.

/**
 * Tests 1=1 returns correct result.
 */
public void testOneEqualsOne() throws Exception {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        // "SELECT e FROM Employee e"
        Query query = em.createQuery(qb.createQuery(Employee.class));
        List emps = query.getResultList();
        assertNotNull(emps);
        int numRead = emps.size();
        // "SELECT e FROM Employee e WHERE 1=1");
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        cq.where(qb.equal(qb.literal(1), 1));
        emps = em.createQuery(cq).getResultList();
        assertNotNull(emps);
        assertEquals(numRead, emps.size());
        ExpressionBuilder builder = new ExpressionBuilder();
        query = ((JpaEntityManager) em.getDelegate()).createQuery(builder.value(1).equal(builder.value(1)), Employee.class);
        emps = query.getResultList();
        assertNotNull(emps);
        assertEquals(numRead, emps.size());
    } 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) TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 33 with CriteriaQuery

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

the class JUnitCriteriaSimpleTestSuiteBase method mapCastTest.

public void mapCastTest() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        BeerConsumer bc1 = new BeerConsumer();
        bc1.setName("George");
        em.persist(bc1);
        Blue blue = new Blue();
        blue.setUniqueKey(new BigInteger("1"));
        em.persist(blue);
        bc1.addBlueBeerToConsume(blue);
        blue.setBeerConsumer(bc1);
        BeerConsumer bc2 = new BeerConsumer();
        bc2.setName("Scott");
        em.persist(bc2);
        BlueLight blueLight = new BlueLight();
        blueLight.setDiscount(10);
        blueLight.setUniqueKey(new BigInteger("2"));
        em.persist(blueLight);
        blueLight.setBeerConsumer(bc2);
        bc2.addBlueBeerToConsume(blueLight);
        em.flush();
        em.clear();
        clearCache();
        ReadAllQuery query = new ReadAllQuery();
        Expression selectionCriteria = new ExpressionBuilder().anyOf("blueBeersToConsume").treat(BlueLight.class).get("discount").equal(10);
        query.setSelectionCriteria(selectionCriteria);
        query.setReferenceClass(BeerConsumer.class);
        query.dontUseDistinct();
        Query jpaQuery = ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) em.getDelegate()).createQuery(query);
        List expectedResult = jpaQuery.getResultList();
        clearCache();
        em.clear();
        // "SELECT e from Employee e join cast(e.project, LargeProject) p where p.budget = 1000
        CriteriaBuilder qb1 = em.getCriteriaBuilder();
        CriteriaQuery<BeerConsumer> cq1 = qb1.createQuery(BeerConsumer.class);
        Root<BeerConsumer> root = wrapper.from(cq1, BeerConsumer.class);
        Join<BeerConsumer, Blue> join = wrapper.join(root, BeerConsumer_blueBeersToConsume);
        jakarta.persistence.criteria.Expression exp = wrapper.get((Path) join.as(BlueLight.class), BlueLight_discount);
        cq1.where(qb1.equal(exp, 10));
        List<BeerConsumer> result = em.createQuery(cq1).getResultList();
        assertTrue("LargeProject cast failed.", comparer.compareObjects(result, expectedResult));
    } finally {
        if (isTransactionActive(em)) {
            this.rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) 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) Blue(org.eclipse.persistence.testing.models.jpa.inherited.Blue) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) BlueLight(org.eclipse.persistence.testing.models.jpa.inherited.BlueLight) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Expression(org.eclipse.persistence.expressions.Expression) BigInteger(java.math.BigInteger) List(java.util.List) ArrayList(java.util.ArrayList) BeerConsumer(org.eclipse.persistence.testing.models.jpa.inherited.BeerConsumer)

Example 34 with CriteriaQuery

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

the class AdvancedCriteriaQueryTestSuite method testSimpleWhereObject.

public void testSimpleWhereObject() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        CriteriaBuilder qb = em.getCriteriaBuilder();
        Root<Employee> root = cq.from(em.getMetamodel().entity(Employee.class));
        cq.where(qb.equal(root.get("firstName"), qb.literal("Bob")));
        TypedQuery<Employee> tq = em.createQuery(cq);
        List<Employee> result = tq.getResultList();
        assertFalse("No Employees were returned", result.isEmpty());
        assertTrue("Did not return Employee", result.get(0).getClass().equals(Employee.class));
        assertTrue("Employee had wrong firstname", result.get(0).getFirstName().equalsIgnoreCase("bob"));
    } 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) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery)

Example 35 with CriteriaQuery

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

the class AdvancedCriteriaQueryTestSuite method testQueryCacheFirstCacheHits.

/**
 * Test that a cache hit will occur on a query.
 */
public void testQueryCacheFirstCacheHits() {
    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.
        cq = qb.createQuery(Employee.class);
        Root<Employee> from = cq.from(Employee.class);
        cq.where(qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName")));
        query = em.createQuery(cq);
        query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheThenDatabase);
        query.setParameter("firstName", employee.getFirstName());
        Employee queryResult = (Employee) query.getSingleResult();
        if (!queryResult.getFirstName().equals(employee.getFirstName())) {
            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 : 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) 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