List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
From source file:com.zero.dao.impl.BaseDaoImpl.java
public List<T> findList(Integer first, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass); criteriaQuery.select(criteriaQuery.from(entityClass)); return findList(criteriaQuery, first, count, filters, orders); }
From source file:com.zero.dao.impl.BaseDaoImpl.java
public Page<T> findPage(Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass); criteriaQuery.select(criteriaQuery.from(entityClass)); return findPage(criteriaQuery, pageable); }
From source file:com.zero.dao.impl.BaseDaoImpl.java
public long count(Filter... filters) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass); criteriaQuery.select(criteriaQuery.from(entityClass)); return count(criteriaQuery, filters != null ? Arrays.asList(filters) : null); }
From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java
public int itemsDateRangeCount(Date start, Date end) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(ActivityAIPC.class); Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class); cq.select(cb.count(rt.get(ActivityAIPC_.actinstproccodeser))); cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(), cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528), cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529), cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530), cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end)))); Query q = em.createQuery(cq); return ((Long) (q.getSingleResult())).intValue(); }
From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java
public List<ActivityAIPC> itemsDateRange(Date start, Date end, int[] range) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(ActivityAIPC.class); Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class); cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(), cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528), cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529), cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530), cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end)))); cq.orderBy(cb.asc(rt.get(ActivityAIPC_.fromdateofservice))); Query q = em.createQuery(cq); q.setMaxResults(range[1] - range[0] + 1); q.setFirstResult(range[0]);/*from w w w.jav a 2 s .c o m*/ return q.getResultList(); }
From source file:com.sfs.captor.controller.RequirementAction.java
/** * Get count of requirement rules//w w w .j a v a 2s . co m * * @return count */ private Long getRequirementRuleCount() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> c = cb.createQuery(Long.class); c.select(cb.count(c.from(RequirementRule.class))); return em.createQuery(c).getSingleResult(); }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * /*from w w w . j a va 2s. c o m*/ * @return */ public List<JiraMetricMeasurement> getAllJiraMetricObjects() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); query.from(JiraMetricMeasurement.class); return em.createQuery(query).getResultList(); }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * list entity by CriteriaInfo/*from www . ja va 2s .co m*/ * * @param criteriaInfo * @return PagedResult */ public PagedResult<T> list(CriteriaInfo criteriaInfo) { EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(entityClass); Root<T> userRoot = cq.from(entityClass); cq.select(userRoot); ParamInfo paramInfo = criteriaInfo.getParamInfo(); PageInfo pageInfo = criteriaInfo.getPageInfo(); SortInfo sortInfo = criteriaInfo.getSortInfo(); //build query for paramInfo if (paramInfo != null) { Set<Predicate> andCriteria = new HashSet(); Set<Predicate> orCriteria = new HashSet(); for (ParamItem item : paramInfo.getParamItems()) { Predicate predicate; if (item.getValue() instanceof String) { //fuzy search for string String regExp = "%" + item.getValue() + "%"; predicate = cb.like((Expression) (userRoot.get(item.getFieldName())), regExp); } else { predicate = cb.equal((userRoot.get(item.getFieldName())), item.getValue()); } switch (item.getOperator()) { case AND: andCriteria.add(predicate); break; case OR: orCriteria.add(predicate); break; } } if (orCriteria.size() > 0) { Predicate or = cb.or(orCriteria.toArray(new Predicate[orCriteria.size()])); andCriteria.add(or); } if (andCriteria.size() > 0) { Predicate and = cb.and(andCriteria.toArray(new Predicate[andCriteria.size()])); cq.where(and); } } //build query for sortInfo Set<Order> orderPredicate = new HashSet<>(); if (sortInfo != null) { for (SortItem item : sortInfo.getSortItems()) { if (item.isDescending()) { orderPredicate.add(cb.desc(userRoot.get(item.getFieldName()))); } else { orderPredicate.add(cb.asc(userRoot.get(item.getFieldName()))); } } } if (orderPredicate.size() > 0) { cq.orderBy(orderPredicate.toArray(new Order[orderPredicate.size()])); } TypedQuery<T> query = em.createQuery(cq); //set result range if (pageInfo != null) { query.setFirstResult(pageInfo.getOffset()); query.setMaxResults(pageInfo.getSize()); } int totalSize; if (paramInfo != null && paramInfo.getParamItems().size() > 0) { totalSize = count(paramInfo); } else { totalSize = count(); } return new PagedResult(query.getResultList(), totalSize); }
From source file:ch.sdi.plugins.oxwall.job.OxSqlJob.java
/** * @see ch.sdi.core.intf.SqlJob#isAlreadyPresent(ch.sdi.core.impl.data.Person) *//* w w w .j a v a 2 s . co m*/ @Override public boolean isAlreadyPresent(Person<?> aPerson) throws SdiException { if (myDryRun && !myCheckDuplicateOnDryRun) { myLog.debug("DryRun is active. Not checking for duplicate person"); return false; } // if myDryRun CriteriaBuilder cb = myEntityManager.getCriteriaBuilder(); CriteriaQuery<OxUser> criteria = cb.createQuery(OxUser.class); Root<OxUser> root = criteria.from(OxUser.class); ParameterExpression<String> mailParam = cb.parameter(String.class); criteria.select(root).where(cb.equal(root.get("email"), mailParam)); TypedQuery<OxUser> queryEMail = myEntityManager.createQuery(criteria); queryEMail.setParameter(mailParam, aPerson.getEMail()); List<OxUser> results = queryEMail.getResultList(); if (results.size() > 0) { myLog.debug("given Person is already present: " + results.get(0)); return true; } // if results.size() > 0 return false; }
From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java
private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, OrderSearchParams params, Set<UUID> processingPeriodIds, Pageable pageable, boolean count, Set<UUID> availableSupplyingFacilities, Set<UUID> availableRequestingFacilities) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); Root<Order> root = query.from(Order.class); if (count) {// w ww. j ava 2s . c o m CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query; query = (CriteriaQuery<T>) countQuery.select(builder.count(root)); } Predicate predicate = builder.conjunction(); predicate = isEqual(SUPPLYING_FACILITY_ID, params.getSupplyingFacilityId(), root, predicate, builder); predicate = isEqual(REQUESTING_FACILITY_ID, params.getRequestingFacilityId(), root, predicate, builder); if (!(isEmpty(availableSupplyingFacilities) && isEmpty(availableRequestingFacilities))) { Predicate orPredicate = builder.disjunction(); orPredicate = isOneOfOr(SUPPLYING_FACILITY_ID, availableSupplyingFacilities, root, orPredicate, builder); orPredicate = isOneOfOr(REQUESTING_FACILITY_ID, availableRequestingFacilities, root, orPredicate, builder); predicate = builder.and(predicate, orPredicate); } predicate = isEqual(PROGRAM_ID, params.getProgramId(), root, predicate, builder); predicate = isOneOf(PROCESSING_PERIOD_ID, processingPeriodIds, root, predicate, builder); predicate = isOneOf(ORDER_STATUS, params.getStatusAsEnum(), root, predicate, builder); query.where(predicate); if (!count && pageable != null && pageable.getSort() != null) { query = addSortProperties(query, root, pageable); } return query; }