コード例 #1
0
 @Test
 public void avg() {
   EntityManager entityManager = factory.createEntityManager();
   CriteriaBuilder builder = entityManager.getCriteriaBuilder();
   CriteriaQuery<Double> query = builder.createQuery(Double.class);
   Root<Employee> root = query.from(Employee.class);
   query.select(builder.avg(root.get(Employee_.age)));
   Double min = entityManager.createQuery(query).getSingleResult();
   assertEquals(new Double(35.625), min);
 }
コード例 #2
0
  /** This test provides a demonstration of the SUM and AVE aggregate functions */
  @Test
  public void testSumAve() {
    log.info("*** testSumAve() ***");

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Number> qdef = cb.createQuery(Number.class);
    Root<Sale> s = qdef.from(Sale.class);

    // select sum(s.amount) from Sale s
    qdef.select(cb.sum(s.<BigDecimal>get("amount")));
    List<Number> results = executeQuery(qdef);
    assertEquals("unexpected number of rows", 1, results.size());
    assertEquals("unexpected result", 250, results.get(0).intValue());

    // select avg(s.amount) from Sale s
    qdef.select(cb.avg(s.<BigDecimal>get("amount")));
    results = executeQuery(qdef);
    assertEquals("unexpected number of rows", 1, results.size());
    assertEquals("unexpected result", 125, results.get(0).intValue());
  }
コード例 #3
0
ファイル: Avg.java プロジェクト: sslavic/query
 @SuppressWarnings("unchecked")
 @Override
 public <R> Selection<X> toSelection(
     CriteriaQuery<R> query, CriteriaBuilder builder, Path<? extends P> path) {
   return (Selection<X>) builder.avg(path.get(attribute));
 }