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();
}
}
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();
}
}
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;
}
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();
}
}
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();
}
}
Aggregations