List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
From source file:com.sfs.captor.controller.RequirementAction.java
/** * find tests associated with this artifact *//*ww w.j a va 2 s.com*/ private List<RequirementRuleTest> findRequirementRuleTests(RequirementRule requirementRule) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<RequirementRuleTest> c = cb.createQuery(RequirementRuleTest.class); Root<RequirementRuleTest> obj = c.from(RequirementRuleTest.class); c.select(obj).where(cb.equal(obj.get("requirementRule"), requirementRule)); return em.createQuery(c).getResultList(); }
From source file:eu.uqasar.service.dataadapter.CubesDataService.java
/** * //from w w w . j ava2 s. c o m * @return */ public List<CubesMetricMeasurement> getAllCubesMetricObjects() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class); query.from(CubesMetricMeasurement.class); return em.createQuery(query).getResultList(); }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * /*from w ww. j a v a 2 s.co m*/ * @param metric * @param date * @return * @throws uQasarException */ public int countMeasurementsByMetricAndDate(String metric, Date date) throws uQasarException { logger.info("Count measurements for metric: " + metric + " and date: " + date); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class); Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class); Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric); Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.timeStamp), date); Predicate condition3 = cb.and(condition, condition2); query.where(condition3); query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp))); Integer res = em.createQuery(query).getResultList().size(); logger.info("Results' count: " + res); return res; }
From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java
/** * Applies the given {@link org.springframework.data.jpa.domain.Specification} to the given {@link javax.persistence.criteria.CriteriaQuery}. * * @param spec can be {@literal null}.//from ww w .j a v a 2 s . c o m * @param query must not be {@literal null}. * @return */ private <S> Root<M> applySpecificationToCriteria(Specification<M> spec, CriteriaQuery<S> query) { Assert.notNull(query); Root<M> root = query.from(entityClass); if (spec == null) { return root; } CriteriaBuilder builder = em.getCriteriaBuilder(); Predicate predicate = spec.toPredicate(root, query, builder); if (predicate != null) { query.where(predicate); } return root; }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * Get the latest date of measurement snapshots * @return//from ww w .j ava2s . co m */ 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:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java
/** * Add the {@link SystemUserLogType} to the database. * * @param log/*from w ww . j a va2s . co m*/ * the {@link SystemUserLogType} to add. */ private void addChannelInvocation(final SystemUserLogType log) { /* Setup context and version. */ log.setContext(this.getContextName()); log.setVersion(this.getContextVersion()); if (log.getUserName() != null && log.getUserName().trim().length() == 0) { log.setUserName(null); } /* Setup to search existing one. */ final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<SystemUserLogType> q = cb.createQuery(SystemUserLogType.class); final Root<SystemUserLogType> c = q.from(SystemUserLogType.class); final List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.equal(c.<String>get(SystemUserLogType_.context), log.getContext())); predicates.add(cb.equal(c.<String>get(SystemUserLogType_.version), log.getVersion())); if (log.getUserName() == null) { predicates.add(cb.isNull(c.<String>get(SystemUserLogType_.userName))); } else { predicates.add(cb.equal(c.<String>get(SystemUserLogType_.userName), log.getUserName())); } predicates.add(cb.equal(c.<String>get(SystemUserLogType_.serviceName), log.getServiceName())); predicates.add(cb.equal(c.<String>get(SystemUserLogType_.operationName), log.getOperationName())); predicates.add(cb.between(c.<Date>get(SystemUserLogType_.logDateItem), getDayStart(log.getLogDateItem()), getDayEnd(log.getLogDateItem()))); q.where(predicates.toArray(new Predicate[predicates.size()])); q.orderBy(cb.desc(c.<Long>get(SystemUserLogType_.hjid))); TypedQuery<SystemUserLogType> typedQuery = this.em.createQuery(q); SystemUserLogType persistence = null; synchronized (StatisticsLoggingDao.class) { try { persistence = typedQuery.getSingleResult(); if (persistence == null) { /* Not found -> persist */ persistence = log; this.setupSystemUserLog(persistence, null); this.em.persist(persistence); } else { /* Found -> add and merge */ this.setupSystemUserLog(persistence, log); this.em.merge(persistence); } } catch (final NoResultException e) { /* Not found -> persist */ persistence = log; this.setupSystemUserLog(persistence, null); this.em.persist(persistence); } catch (final NonUniqueResultException e) { /* Found more */ typedQuery = this.em.createQuery(q); final List<SystemUserLogType> list = typedQuery.getResultList(); SystemUserLogType l; for (int i = 0; i < list.size(); i++) { l = list.get(i); if (persistence == null && l.getHjid() != null) { persistence = l; break; } } if (persistence != null) { /* Found more -> condense to first valid one -> merge. */ this.setupSystemUserLog(persistence, log); for (int i = list.size() - 1; i >= 0; i--) { l = list.get(i); if (l != null && l.getHjid() != null) { if (persistence.getHjid().equals(l.getHjid())) { } else { this.setupSystemUserLog(persistence, l); list.remove(i); this.em.remove(l); } } } this.em.merge(persistence); } else { /* Found more -> no valid one in list -> persist. */ persistence = log; this.setupSystemUserLog(persistence, null); this.em.persist(persistence); } } } this.logger.debug("addChannelInvocation SystemUserLog {} {} {} {} {} {}", this.contextNameProvider.getContextName(), this.contextNameProvider.getContextVersion(), String.valueOf(persistence.getUserName()), persistence.getServiceName(), persistence.getOperationName(), persistence.getLogDate().toXMLFormat()); }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public Page<Order> findPage(Order.Type type, Order.Status status, Member member, Goods goods, Boolean isPendingReceive, Boolean isPendingRefunds, Boolean isUseCouponCode, Boolean isExchangePoint, Boolean isAllocatedStock, Boolean hasExpired, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(root);/*from w ww .j a va 2 s . c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (type != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("type"), type)); } if (status != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("status"), status)); } if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (goods != null) { Subquery<Product> productSubquery = criteriaQuery.subquery(Product.class); Root<Product> productSubqueryRoot = productSubquery.from(Product.class); productSubquery.select(productSubqueryRoot); productSubquery.where(criteriaBuilder.equal(productSubqueryRoot.get("goods"), goods)); Subquery<OrderItem> orderItemSubquery = criteriaQuery.subquery(OrderItem.class); Root<OrderItem> orderItemSubqueryRoot = orderItemSubquery.from(OrderItem.class); orderItemSubquery.select(orderItemSubqueryRoot); orderItemSubquery.where(criteriaBuilder.equal(orderItemSubqueryRoot.get("order"), root), criteriaBuilder.in(orderItemSubqueryRoot.get("product")).value(productSubquery)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(orderItemSubquery)); } if (isPendingReceive != null) { Predicate predicate = criteriaBuilder.and( criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date())), criteriaBuilder.equal(root.get("paymentMethodType"), PaymentMethod.Type.cashOnDelivery), criteriaBuilder.notEqual(root.get("status"), Order.Status.completed), criteriaBuilder.notEqual(root.get("status"), Order.Status.failed), criteriaBuilder.notEqual(root.get("status"), Order.Status.canceled), criteriaBuilder.notEqual(root.get("status"), Order.Status.denied), criteriaBuilder.lessThan(root.<BigDecimal>get("amountPaid"), root.<BigDecimal>get("amount"))); if (isPendingReceive) { restrictions = criteriaBuilder.and(restrictions, predicate); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate)); } } if (isPendingRefunds != null) { Predicate predicate = criteriaBuilder.or( criteriaBuilder.and( criteriaBuilder.or( criteriaBuilder.and(root.get("expire").isNotNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"), new Date())), criteriaBuilder.equal(root.get("status"), Order.Status.failed), criteriaBuilder.equal(root.get("status"), Order.Status.canceled), criteriaBuilder.equal(root.get("status"), Order.Status.denied)), criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"), BigDecimal.ZERO)), criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), Order.Status.completed), criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"), root.<BigDecimal>get("amount")))); if (isPendingRefunds) { restrictions = criteriaBuilder.and(restrictions, predicate); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate)); } } if (isUseCouponCode != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUseCouponCode"), isUseCouponCode)); } if (isExchangePoint != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isExchangePoint"), isExchangePoint)); } if (isAllocatedStock != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isAllocatedStock"), isAllocatedStock)); } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date()))); } } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public Long count(Order.Type type, Order.Status status, Member member, Goods goods, Boolean isPendingReceive, Boolean isPendingRefunds, Boolean isUseCouponCode, Boolean isExchangePoint, Boolean isAllocatedStock, Boolean hasExpired) {/*from w w w. ja v a 2s. com*/ CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (type != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("type"), type)); } if (status != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("status"), status)); } if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (goods != null) { Subquery<Product> productSubquery = criteriaQuery.subquery(Product.class); Root<Product> productSubqueryRoot = productSubquery.from(Product.class); productSubquery.select(productSubqueryRoot); productSubquery.where(criteriaBuilder.equal(productSubqueryRoot.get("goods"), goods)); Subquery<OrderItem> orderItemSubquery = criteriaQuery.subquery(OrderItem.class); Root<OrderItem> orderItemSubqueryRoot = orderItemSubquery.from(OrderItem.class); orderItemSubquery.select(orderItemSubqueryRoot); orderItemSubquery.where(criteriaBuilder.equal(orderItemSubqueryRoot.get("order"), root), criteriaBuilder.in(orderItemSubqueryRoot.get("product")).value(productSubquery)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(orderItemSubquery)); } if (isPendingReceive != null) { Predicate predicate = criteriaBuilder.and( criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date())), criteriaBuilder.equal(root.get("paymentMethodType"), PaymentMethod.Type.cashOnDelivery), criteriaBuilder.notEqual(root.get("status"), Order.Status.completed), criteriaBuilder.notEqual(root.get("status"), Order.Status.failed), criteriaBuilder.notEqual(root.get("status"), Order.Status.canceled), criteriaBuilder.notEqual(root.get("status"), Order.Status.denied), criteriaBuilder.lessThan(root.<BigDecimal>get("amountPaid"), root.<BigDecimal>get("amount"))); if (isPendingReceive) { restrictions = criteriaBuilder.and(restrictions, predicate); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate)); } } if (isPendingRefunds != null) { Predicate predicate = criteriaBuilder.or( criteriaBuilder.and( criteriaBuilder.or( criteriaBuilder.and(root.get("expire").isNotNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"), new Date())), criteriaBuilder.equal(root.get("status"), Order.Status.failed), criteriaBuilder.equal(root.get("status"), Order.Status.canceled), criteriaBuilder.equal(root.get("status"), Order.Status.denied)), criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"), BigDecimal.ZERO)), criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), Order.Status.completed), criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"), root.<BigDecimal>get("amount")))); if (isPendingRefunds) { restrictions = criteriaBuilder.and(restrictions, predicate); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate)); } } if (isUseCouponCode != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUseCouponCode"), isUseCouponCode)); } if (isExchangePoint != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isExchangePoint"), isExchangePoint)); } if (isAllocatedStock != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isAllocatedStock"), isAllocatedStock)); } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date()))); } } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public List<Order> findList(Order.Type type, Order.Status status, Member member, Goods goods, Boolean isPendingReceive, Boolean isPendingRefunds, Boolean isUseCouponCode, Boolean isExchangePoint, Boolean isAllocatedStock, Boolean hasExpired, Integer count, List<Filter> filters, List<net.shopxx.Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(root);/*from w w w . j a va 2 s. c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (type != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("type"), type)); } if (status != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("status"), status)); } if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (goods != null) { Subquery<Product> productSubquery = criteriaQuery.subquery(Product.class); Root<Product> productSubqueryRoot = productSubquery.from(Product.class); productSubquery.select(productSubqueryRoot); productSubquery.where(criteriaBuilder.equal(productSubqueryRoot.get("goods"), goods)); Subquery<OrderItem> orderItemSubquery = criteriaQuery.subquery(OrderItem.class); Root<OrderItem> orderItemSubqueryRoot = orderItemSubquery.from(OrderItem.class); orderItemSubquery.select(orderItemSubqueryRoot); orderItemSubquery.where(criteriaBuilder.equal(orderItemSubqueryRoot.get("order"), root), criteriaBuilder.in(orderItemSubqueryRoot.get("product")).value(productSubquery)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(orderItemSubquery)); } if (isPendingReceive != null) { Predicate predicate = criteriaBuilder.and( criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date())), criteriaBuilder.equal(root.get("paymentMethodType"), PaymentMethod.Type.cashOnDelivery), criteriaBuilder.notEqual(root.get("status"), Order.Status.completed), criteriaBuilder.notEqual(root.get("status"), Order.Status.failed), criteriaBuilder.notEqual(root.get("status"), Order.Status.canceled), criteriaBuilder.notEqual(root.get("status"), Order.Status.denied), criteriaBuilder.lessThan(root.<BigDecimal>get("amountPaid"), root.<BigDecimal>get("amount"))); if (isPendingReceive) { restrictions = criteriaBuilder.and(restrictions, predicate); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate)); } } if (isPendingRefunds != null) { Predicate predicate = criteriaBuilder.or( criteriaBuilder.and( criteriaBuilder.or( criteriaBuilder.and(root.get("expire").isNotNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"), new Date())), criteriaBuilder.equal(root.get("status"), Order.Status.failed), criteriaBuilder.equal(root.get("status"), Order.Status.canceled), criteriaBuilder.equal(root.get("status"), Order.Status.denied)), criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"), BigDecimal.ZERO)), criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), Order.Status.completed), criteriaBuilder.greaterThan(root.<BigDecimal>get("amountPaid"), root.<BigDecimal>get("amount")))); if (isPendingRefunds) { restrictions = criteriaBuilder.and(restrictions, predicate); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(predicate)); } } if (isUseCouponCode != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUseCouponCode"), isUseCouponCode)); } if (isExchangePoint != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isExchangePoint"), isExchangePoint)); } if (isAllocatedStock != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isAllocatedStock"), isAllocatedStock)); } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, root.get("expire").isNotNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("expire"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(), criteriaBuilder.greaterThan(root.<Date>get("expire"), new Date()))); } } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:eu.uqasar.service.dataadapter.JiraDataService.java
/** * //from w ww .j av a 2s .c o m * @param metric * @return * @throws uQasarException */ public int countMeasurementsPerProjectByMetricWithinPeriod(Project project, String metric, String period) throws uQasarException { logger.info("Count measurements for 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); Predicate condition4 = cb.and(condition1, condition2, condition3); query.where(condition4); query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp))); Integer res = em.createQuery(query).getResultList().size(); logger.info("Results' count: " + res); return res; }