@Test
 public void testDiff() {
   EntityManager em = getOrCreateEntityManager();
   em.getTransaction().begin();
   CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
   criteria.from(Product.class);
   criteria.select(builder.diff(builder.literal(5), builder.literal(2)));
   Integer result = em.createQuery(criteria).getSingleResult();
   assertEquals(Integer.valueOf(3), result);
   em.getTransaction().commit();
   em.close();
 }
Esempio n. 2
0
  @Test
  public void diff() {
    EntityManager entityManager = factory.createEntityManager();
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
    Root<Employee> root = query.from(Employee.class);
    query.select(root);

    query.where(builder.lessThan(builder.diff(root.get(Employee_.age), 10), 10));

    List<Employee> employees = entityManager.createQuery(query).getResultList();
    assertAndShow(4, employees);
  }
 @Test
 public void testDiffWithQuotient() {
   EntityManager em = getOrCreateEntityManager();
   em.getTransaction().begin();
   CriteriaQuery<Number> criteria = builder.createQuery(Number.class);
   criteria.from(Product.class);
   criteria.select(
       builder.quot(
           builder.diff(
               builder.literal(BigDecimal.valueOf(2.0)), builder.literal(BigDecimal.valueOf(1.0))),
           BigDecimal.valueOf(2.0)));
   Number result = em.createQuery(criteria).getSingleResult();
   assertEquals(0.5d, result.doubleValue(), 0.1d);
   em.getTransaction().commit();
   em.close();
 }