Package javax.persistence.criteria

Examples of javax.persistence.criteria.QueryBuilder


        TypedQuery<Order> jpqlTypedQuery = em.createQuery("Select o from Order o where o.filled = true", Order.class);
        List<Order> jpqlTypedResults = jpqlTypedQuery.getResultList();
        assertEquals(N_ORDERS / 2, jpqlTypedResults.size());

        // create the same query and get typed results.
        QueryBuilder qb = em.getQueryBuilder();
        CriteriaQuery<Order> cq = qb.createQuery(Order.class);
        Root<Order> order = cq.from(Order.class);
        cq.select(order).where(qb.equal(order.get(Order_.filled), Boolean.TRUE));

        TypedQuery<Order> typedCriteriaQuery = em.createQuery(cq);
        List<Order> typedCriteriaResults = typedCriteriaQuery.getResultList();
        assertEquals(N_ORDERS / 2, typedCriteriaResults.size());
View Full Code Here


        TypedQuery<Order> typedJpqlQuery = em.createQuery("Select o from Order o where o.date < :maxDate", Order.class);
        typedJpqlQuery.setParameter("maxDate", maxDate);
        List<Order> typedJpqlResults = typedJpqlQuery.getResultList();
        assertEquals(N_ORDERS / 2, typedJpqlResults.size());

        QueryBuilder qb = em.getQueryBuilder();
        CriteriaQuery<Order> criteriaQuery = qb.createQuery(Order.class);
        Root<Order> order = criteriaQuery.from(Order.class);
        criteriaQuery.select(order).where(qb.lessThan(order.get(Order_.date), qb.parameter(Date.class, "maxDate")));
        TypedQuery<Order> tq = em.createQuery(criteriaQuery);
        tq.setParameter("maxDate", maxDate);
        List<Order> criteriaResults = tq.getResultList();
        assertEquals(N_ORDERS / 2, criteriaResults.size());
View Full Code Here

            em.createQuery("SELECT o, p from Order o JOIN o.items i JOIN i.producer p WHERE o.filled = true");
        // don't suppress warnings.
        List<Object[]> jpqlResults = jpqlQuery.getResultList();

        // Get results using Tuple
        QueryBuilder qb = em.getQueryBuilder();
        CriteriaQuery<Tuple> criteriaQuery = qb.createTupleQuery();
        Root<Order> order = criteriaQuery.from(Order.class);
        Join<Item, Producer> producer = order.join(Order_.items).join(Item_.producer);
        criteriaQuery.select(qb.tuple(order, producer));
        criteriaQuery.where(qb.equal(order.get(Order_.filled), Boolean.TRUE));
        TypedQuery<Tuple> eq = em.createQuery(criteriaQuery);
        List<Tuple> criteriaResults = eq.getResultList();

        // Get results using custom class
        CriteriaQuery<OrderProducer> constructQuery = qb.createQuery(OrderProducer.class);
        Root<Order> order2 = constructQuery.from(Order.class);
        Join<Item, Producer> producer2 = order.join(Order_.items).join(Item_.producer);
        constructQuery.select(qb.construct(OrderProducer.class, order2, producer2));
        constructQuery.where(qb.equal(order2.get(Order_.filled), Boolean.TRUE));
        TypedQuery<OrderProducer> typedQuery = em.createQuery(constructQuery);
        List<OrderProducer> constructResults = typedQuery.getResultList();

        assertEquals(N_ORDERS / 2 * N_ITEMS_PER_ORDER, jpqlResults.size());
        assertEquals(N_ORDERS / 2 * N_ITEMS_PER_ORDER, criteriaResults.size());
View Full Code Here

        SingularAttribute<SimpleEntity, Long> id =
            (SingularAttribute<SimpleEntity, Long>)entity.getSingularAttribute("id");
        SingularAttribute<SimpleEntity, String> name =
            (SingularAttribute<SimpleEntity, String>)entity.getSingularAttribute("name");
       
        QueryBuilder cb = oemf.getQueryBuilder();
        CriteriaQuery<SimpleEntity> c = cb.createQuery(SimpleEntity.class);
        Root<SimpleEntity> root = c.from(SimpleEntity.class);
        ParameterExpression<Long> param1 = cb.parameter(long.class);
        ParameterExpression<String> param2 = cb.parameter(String.class);
        Predicate p1 = cb.equal(root.get(id), param1);
        Predicate p2 = cb.equal(root.get(name), param2);
        c.where(cb.and(p1,p2));
       
        Query q = em.createQuery(c);
        assertEquals(2, q.getParameters().size());
        assertTrue(q.getParameters().contains(param1));
        assertTrue(q.getParameters().contains(param2));
View Full Code Here

TOP

Related Classes of javax.persistence.criteria.QueryBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.