use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class ExpressionsTest method testSumWithQuotient.
@Test
public void testSumWithQuotient() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Number> criteria = builder.createQuery(Number.class);
criteria.from(Product.class);
criteria.select(builder.quot(builder.sum(builder.literal(BigDecimal.valueOf(0.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();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class ExpressionsTest method testDiffWithQuotient.
@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();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class ExpressionsTest method testParameterReuse.
@Test
public void testParameterReuse() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Product> criteria = em.getCriteriaBuilder().createQuery(Product.class);
Root<Product> from = criteria.from(Product.class);
ParameterExpression<String> param = em.getCriteriaBuilder().parameter(String.class);
Predicate predicate = em.getCriteriaBuilder().equal(from.get(Product_.id), param);
Predicate predicate2 = em.getCriteriaBuilder().equal(from.get(Product_.name), param);
criteria.where(em.getCriteriaBuilder().or(predicate, predicate2));
assertEquals(1, criteria.getParameters().size());
TypedQuery<Product> query = em.createQuery(criteria);
int hqlParamCount = countGeneratedParameters(query.unwrap(Query.class));
assertEquals(1, hqlParamCount);
query.setParameter(param, "abc").getResultList();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class ExpressionsTest method testInExplicitTupleList.
@Test
public void testInExplicitTupleList() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
Root<Product> from = criteria.from(Product.class);
criteria.where(from.get(Product_.partNumber).in(Collections.singletonList(((long) Integer.MAX_VALUE) + 1)));
List<Product> result = em.createQuery(criteria).getResultList();
assertEquals(1, result.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class ExpressionsTest method testEmptyInList.
@Test
@TestForIssue(jiraKey = "HHH-6876")
@RequiresDialect(H2Dialect.class)
public void testEmptyInList() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
Root<Product> from = criteria.from(Product.class);
// empty IN list
criteria.where(from.get(Product_.partNumber).in());
List<Product> result = em.createQuery(criteria).getResultList();
assertEquals(0, result.size());
em.getTransaction().commit();
em.close();
}
Aggregations