List of usage examples for javax.persistence.criteria Root get
<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public Integer getSalesVolume(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.join("orderItems").<Integer>get("shippedQuantity"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/* ww w . j a va2s . c o m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java
public List<M> findAll(Iterable<ID> ids) { return getQuery(new Specification<M>() { public Predicate toPredicate(Root<M> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<?> path = root.get(entityInformation.getIdAttribute()); return path.in(cb.parameter(Iterable.class, "ids")); }/*from w w w .j a v a 2 s.c om*/ }, (Sort) null).setParameter("ids", ids).getResultList(); }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * Get the latest date of measurement snapshots * @return/*from w ww. j a v a 2 s . com*/ */ public Date getLatestDate() { logger.info("Get the latest date from JIRA measurements..."); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class); query.select(root); query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp))); Date latest; try { JiraMetricMeasurement m = em.createQuery(query).setMaxResults(1).getSingleResult(); latest = m.getTimeStamp(); } catch (NoResultException nre) { latest = null; } return latest; }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * /*from w ww. j a v a2 s. com*/ * @param metric * @return * @throws uQasarException */ public List<JiraMetricMeasurement> getMeasurementsPerProjectByMetricWithinPeriod(Project project, String metric, String period) throws uQasarException { List<JiraMetricMeasurement> measurements = new ArrayList<>(); if (project != null && metric != null && period != null) { logger.info( "Count measurements for project + " + project.getAbbreviatedName() + " and metric: " + metric); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class); Predicate condition1 = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric); Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.project), project); Date from = getDateForPeriod(period); Date to = DateTime.now().toDate(); Predicate condition3 = cb.between(root.get(JiraMetricMeasurement_.timeStamp), from, to); // System.out.println("from:"+from); // System.out.println("to:"+to); Predicate condition4 = cb.and(condition1, condition2, condition3); query.where(condition4); query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp))); measurements = em.createQuery(query).getResultList(); } return measurements; }
From source file:com.zero.dao.impl.BaseDaoImpl.java
protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) { Assert.notNull(criteriaQuery);// www . j ava 2 s . c o m Assert.notNull(criteriaQuery.getSelection()); Assert.notEmpty(criteriaQuery.getRoots()); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); addRestrictions(criteriaQuery, filters); CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class); for (Root<?> root : criteriaQuery.getRoots()) { Root<?> dest = countCriteriaQuery.from(root.getJavaType()); dest.alias(getAlias(root)); copyJoins(root, dest); } Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType()); countCriteriaQuery.select(criteriaBuilder.count(countRoot.get("id").<String>get("stcd"))); if (criteriaQuery.getGroupList() != null) { countCriteriaQuery.groupBy(criteriaQuery.getGroupList()); } if (criteriaQuery.getGroupRestriction() != null) { countCriteriaQuery.having(criteriaQuery.getGroupRestriction()); } if (criteriaQuery.getRestriction() != null) { countCriteriaQuery.where(criteriaQuery.getRestriction()); } return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:com.carser.viamais.vo.TransactionFilter.java
public List<Transaction> resultList(final EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class); Root<Transaction> transaction = cq.from(Transaction.class); cq.where(getPredicates(cb, transaction)); @SuppressWarnings("rawtypes") Path path;/*from w w w .j a v a2 s .c o m*/ switch (orderBy) { case CUSTOMER_NAME: path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name); break; case DATE: path = transaction.get(Transaction_.transactionDate); break; case PLATE: path = transaction.get(Transaction_.car).get(Car_.licensePlate); break; case TYPE: path = transaction.get(Transaction_.type); break; case YEAR: path = transaction.get(Transaction_.car).get(Car_.yearOfModel); break; default: path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name); break; } switch (order) { case DESC: cq.orderBy(cb.desc(path)); break; default: cq.orderBy(cb.asc(path)); break; } TypedQuery<Transaction> query = em.createQuery(cq); query.setFirstResult(offset); query.setMaxResults(limit); return query.getResultList(); }
From source file:com.sapito.db.dao.AbstractDao.java
/** * Obtiene todos los registros para una entidad dada ordenados con los * criterios establecidos a traves de la lista <code>ordering</code> * // w w w.j a v a 2 s .c om * @param ordering : ["fieldName", "ASC" | "DESC"] <br> * (if != "ASC" se utiliza "DESC" por default) * * @return Registros recuperados ordenados mediante los criterios dados */ public List<T> findAll(LinkedHashMap<String, String> ordering) { CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(); Root<T> root = cq.from(entityClass); cq.select(root); if (ordering != null) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); Set<String> set = ordering.keySet(); List<Order> orders = new ArrayList<>(); for (String orderingField : set) { Order order = (ordering.get(orderingField).equals("ASC")) ? cb.asc(root.get(orderingField)) : cb.desc(root.get(orderingField)); orders.add(order); } cq.orderBy(orders); } return entityManager.createQuery(cq).setMaxResults(MAX_RECORDS_RETURNED).getResultList(); }
From source file:eu.uqasar.service.dataadapter.CubesDataService.java
/** * // www . j a v a2s.co m * @param first * @param count * @return */ public List<CubesMetricMeasurement> getAllByAscendingName(int first, int count) { logger.infof("loading all CubesMetricMeasurement ordered by ascending name ..."); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<CubesMetricMeasurement> criteria = cb.createQuery(CubesMetricMeasurement.class); Root<CubesMetricMeasurement> root = criteria.from(CubesMetricMeasurement.class); criteria.orderBy(cb.asc(root.get(CubesMetricMeasurement_.self))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:eu.uqasar.service.dataadapter.CubesDataService.java
/** * Count the measurement results belonging to a specific adapter * @param adapter//from ww w . j av a 2 s . c o m * @return */ public int countAllByAdapter(AdapterSettings adapter) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class); Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class); Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.adapter), adapter); query.where(condition); return em.createQuery(query).getResultList().size(); }
From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java
@Override public Specification<Device> isManagedExternally(final Boolean isManagedExternally) throws ArgumentNullOrEmptyException { if (isManagedExternally == null) { throw new ArgumentNullOrEmptyException("isManagedExternally"); }//from w ww. ja va 2 s . c o m return new Specification<Device>() { @Override public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) { final Subquery<Long> subquery = query.subquery(Long.class); final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class); subquery.select(cb.countDistinct(deviceAuthorizationRoot)); subquery.where(cb.equal(deviceAuthorizationRoot.get("device"), deviceRoot.<Long>get("id"))); if (isManagedExternally) { return cb.greaterThan(subquery, Long.valueOf(1)); } else { return cb.lessThanOrEqualTo(subquery, Long.valueOf(1)); } } }; }