use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.
the class ExpressionsTest method prepareTestData.
@BeforeEach
public void prepareTestData() {
builder = entityManagerFactory().getCriteriaBuilder();
doInJPA(this::entityManagerFactory, entityManager -> {
Product product = new Product();
product.setId("product1");
product.setPrice(1.23d);
product.setQuantity(2);
product.setPartNumber(((long) Integer.MAX_VALUE) + 1);
product.setRating(1.999f);
product.setSomeBigInteger(BigInteger.valueOf(987654321));
product.setSomeBigDecimal(BigDecimal.valueOf(987654.32));
entityManager.persist(product);
});
}
use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.
the class QueryBuilderTest method testTypeConversion.
@Test
public void testTypeConversion() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
Metamodel mm = em.getMetamodel();
EntityType<Product> Product_ = mm.entity(Product.class);
// toFloat
CriteriaQuery<Float> floatQuery = cb.createQuery(Float.class);
Root<Product> product = floatQuery.from(Product.class);
floatQuery.select(cb.toFloat(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
em.createQuery(floatQuery).getResultList();
// toDouble
CriteriaQuery<Double> doubleQuery = cb.createQuery(Double.class);
product = doubleQuery.from(Product.class);
doubleQuery.select(cb.toDouble(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
em.createQuery(doubleQuery).getResultList();
em.getTransaction().commit();
em.close();
}
Aggregations