Search in sources :

Example 56 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.

the class CriteriaStringsTest method testBasicWithFilterLessThan.

/**
 * Test basic generation of query with candidate and alias and filter (<).
 */
public void testBasicWithFilterLessThan() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Person> crit = cb.createQuery(Person.class);
        Root<Person> candidate = crit.from(Person.class);
        candidate.alias("p");
        crit.select(candidate);
        Path ageField = candidate.get("age");
        Predicate ageLessThan = cb.lessThan(ageField, 36);
        crit.where(ageLessThan);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE (p.age < 36)", crit.toString());
        Query q = em.createQuery(crit);
        List<Person> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        Iterator<Person> iter = results.iterator();
        boolean fredFlintstonePresent = false;
        while (iter.hasNext()) {
            Person pers = iter.next();
            if (pers.getFirstName().equals("Fred") && pers.getLastName().equals("Flintstone") && pers.getPersonNum() == 101) {
                fredFlintstonePresent = true;
            }
        }
        assertTrue("Fred was not returned!", fredFlintstonePresent);
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Person(org.datanucleus.samples.annotations.models.company.Person) Predicate(javax.persistence.criteria.Predicate)

Example 57 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project tests by datanucleus.

the class CriteriaStringsTest method testBasicWithFromJoinOneToMany.

/**
 * Test basic generation of query with candidate and alias, and FROM join on 1-N.
 */
public void testBasicWithFromJoinOneToMany() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
        Root<Farm> candidate = crit.from(Farm.class);
        candidate.alias("f");
        crit.select(candidate);
        Metamodel model = emf.getMetamodel();
        ManagedType farmType = model.managedType(Farm.class);
        Attribute animalAttr = farmType.getAttribute("animals");
        Join animalJoin = candidate.join((ListAttribute) animalAttr);
        animalJoin.alias("a");
        Path nameField = animalJoin.get("name");
        Predicate nameEquals = cb.equal(nameField, "Woolly Sheep");
        crit.where(nameEquals);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT f FROM org.datanucleus.samples.annotations.one_many.bidir.Farm f JOIN f.animals a WHERE (a.name = 'Woolly Sheep')", crit.toString());
        Query q = em.createQuery(crit);
        List<Farm> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        Farm farm = (Farm) results.get(0);
        assertEquals("Farm is incorrect", "Kiwi Farm", farm.getName());
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityTransaction(javax.persistence.EntityTransaction) ManagedType(javax.persistence.metamodel.ManagedType) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Join(javax.persistence.criteria.Join) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm) Metamodel(javax.persistence.metamodel.Metamodel)

Example 58 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project jbpm by kiegroup.

the class ExecutorQueryCriteriaUtil method createQueryAndCallApplyMetaCriteriaAndGetResult.

@Override
protected <T> List<T> createQueryAndCallApplyMetaCriteriaAndGetResult(QueryWhere queryWhere, CriteriaQuery<T> criteriaQuery, CriteriaBuilder builder) {
    EntityManager em = getEntityManager();
    Object newTx = joinTransaction(em);
    Query query = em.createQuery(criteriaQuery);
    applyMetaCriteriaToQuery(query, queryWhere);
    // execute query
    List<T> result = query.getResultList();
    closeEntityManager(em, newTx);
    return result;
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) COMMAND_NAME_LIST(org.kie.internal.query.QueryParameterIdentifiers.COMMAND_NAME_LIST) DEPLOYMENT_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.DEPLOYMENT_ID_LIST) EXECUTOR_TIME_LIST(org.kie.internal.query.QueryParameterIdentifiers.EXECUTOR_TIME_LIST) EXECUTOR_KEY_LIST(org.kie.internal.query.QueryParameterIdentifiers.EXECUTOR_KEY_LIST) EXECUTOR_EXECUTIONS_LIST(org.kie.internal.query.QueryParameterIdentifiers.EXECUTOR_EXECUTIONS_LIST) STACK_TRACE_LIST(org.kie.internal.query.QueryParameterIdentifiers.STACK_TRACE_LIST) EXECUTOR_STATUS_LIST(org.kie.internal.query.QueryParameterIdentifiers.EXECUTOR_STATUS_LIST) EXECUTOR_OWNER_LIST(org.kie.internal.query.QueryParameterIdentifiers.EXECUTOR_OWNER_LIST) ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.ID_LIST) EXECUTOR_RETRIES_LIST(org.kie.internal.query.QueryParameterIdentifiers.EXECUTOR_RETRIES_LIST) MESSAGE_LIST(org.kie.internal.query.QueryParameterIdentifiers.MESSAGE_LIST)

Example 59 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project BroadleafCommerce by BroadleafCommerce.

the class PageDaoImpl method readAllPages.

@Override
public List<Page> readAllPages() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Page> criteria = builder.createQuery(Page.class);
    Root<PageImpl> page = criteria.from(PageImpl.class);
    criteria.select(page);
    try {
        Query query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        return query.getResultList();
    } catch (NoResultException e) {
        return Collections.emptyList();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.broadleafcommerce.cms.page.domain.PageImpl) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Page(org.broadleafcommerce.cms.page.domain.Page) NoResultException(javax.persistence.NoResultException)

Example 60 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project BroadleafCommerce by BroadleafCommerce.

the class PageDaoImpl method readAllPageTemplates.

@Override
public List<PageTemplate> readAllPageTemplates() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<PageTemplate> criteria = builder.createQuery(PageTemplate.class);
    Root<PageTemplateImpl> template = criteria.from(PageTemplateImpl.class);
    criteria.select(template);
    try {
        Query query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        return query.getResultList();
    } catch (NoResultException e) {
        return Collections.emptyList();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) PageTemplate(org.broadleafcommerce.cms.page.domain.PageTemplate) PageTemplateImpl(org.broadleafcommerce.cms.page.domain.PageTemplateImpl) NoResultException(javax.persistence.NoResultException)

Aggregations

CriteriaQuery (javax.persistence.criteria.CriteriaQuery)155 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)112 EntityManager (javax.persistence.EntityManager)109 Query (javax.persistence.Query)109 TypedQuery (javax.persistence.TypedQuery)69 Predicate (javax.persistence.criteria.Predicate)51 EntityTransaction (javax.persistence.EntityTransaction)45 Root (javax.persistence.criteria.Root)40 List (java.util.List)22 Path (javax.persistence.criteria.Path)18 Test (org.testng.annotations.Test)16 UserTransaction (javax.transaction.UserTransaction)15 Person (org.datanucleus.samples.annotations.models.company.Person)15 Manager (org.datanucleus.samples.jpa.query.Manager)14 Specification (org.springframework.data.jpa.domain.Specification)14 Item (org.jpwh.model.querying.Item)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 Join (javax.persistence.criteria.Join)10 Session (org.hibernate.Session)9