Search in sources :

Example 11 with Product

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);
    });
}
Also used : Product(org.hibernate.orm.test.jpa.metamodel.Product) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with 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();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Product(org.hibernate.orm.test.jpa.metamodel.Product) Metamodel(jakarta.persistence.metamodel.Metamodel) Test(org.junit.Test)

Aggregations

Product (org.hibernate.orm.test.jpa.metamodel.Product)12 EntityManager (jakarta.persistence.EntityManager)9 Test (org.junit.jupiter.api.Test)8 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)7 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)3 Metamodel (jakarta.persistence.metamodel.Metamodel)2 BigInteger (java.math.BigInteger)2 Test (org.junit.Test)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 TypedQuery (jakarta.persistence.TypedQuery)1 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 Predicate (jakarta.persistence.criteria.Predicate)1 BigDecimal (java.math.BigDecimal)1 AbstractCriteriaTest (org.hibernate.orm.test.jpa.criteria.AbstractCriteriaTest)1 Query (org.hibernate.query.Query)1 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)1 SkipForDialect (org.hibernate.testing.SkipForDialect)1 TestForIssue (org.hibernate.testing.TestForIssue)1