use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.
the class AggregationResultTest method testSumOfLongs.
/**
* Sum of Longs should return a Long
*/
@Test
public void testSumOfLongs() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
Root<Product> productRoot = criteria.from(Product.class);
criteria.select(builder.sum(productRoot.get(Product_.partNumber)));
Object sumResult = em.createQuery(criteria).getSingleResult();
assertReturnType(Long.class, sumResult);
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.
the class AggregationResultTest method testSumOfIntegers.
/**
* Sum of Integers should return an Integer; note that this is distinctly different than JPAQL
*/
@Test
public void testSumOfIntegers() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
Root<Product> productRoot = criteria.from(Product.class);
criteria.select(builder.sum(productRoot.get(Product_.quantity)));
Object sumResult = em.createQuery(criteria).getSingleResult();
assertReturnType(Integer.class, sumResult);
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.
the class AggregationResultTest method testSumOfDoubles.
/**
* Sum of Doubles should return a Double
*/
@Test
public void testSumOfDoubles() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Double> criteria = builder.createQuery(Double.class);
Root<Product> productRoot = criteria.from(Product.class);
criteria.select(builder.sum(productRoot.get(Product_.price)));
Object sumResult = em.createQuery(criteria).getSingleResult();
assertReturnType(Double.class, sumResult);
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.
the class AggregationResultTest method createTestData.
@BeforeEach
public void createTestData() {
builder = entityManagerFactory().getCriteriaBuilder();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Product product = new Product();
product.setId("product1");
product.setPrice(1.23d);
product.setQuantity(1000);
product.setPartNumber(((long) Integer.MAX_VALUE) + 1);
product.setRating(1.999f);
product.setSomeBigInteger(BigInteger.valueOf(987654321));
product.setSomeBigDecimal(BigDecimal.valueOf(987654.32));
em.persist(product);
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.
the class QueryBuilderTest method testEqualityComparisonLiteralConversion.
@Test
public void testEqualityComparisonLiteralConversion() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
Metamodel mm = em.getMetamodel();
CriteriaQuery<Integer> cquery = cb.createQuery(Integer.class);
Root<Product> product = cquery.from(Product.class);
EntityType<Product> Product_ = mm.entity(Product.class);
cquery.select(cb.toInteger(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
SqmComparisonPredicate predicate = (SqmComparisonPredicate) cb.equal(product.get(Product_.getSingularAttribute("partNumber", Long.class)), 373767373);
assertEquals(Long.class, predicate.getLeftHandExpression().getJavaType());
cquery.where(predicate);
em.createQuery(cquery).getResultList();
predicate = (SqmComparisonPredicate) cb.ge(cb.length(product.get(Product_.getSingularAttribute("name", String.class))), 4L);
assertEquals(Integer.class, predicate.getLeftHandExpression().getJavaType());
cquery.where(predicate);
em.createQuery(cquery).getResultList();
em.getTransaction().commit();
em.close();
}
Aggregations