Search in sources :

Example 6 with Product

use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.

the class AggregationResultTest method testSumOfBigIntegers.

/**
 * Sum of BigInteger should return a BigInteger
 */
@Test
public void testSumOfBigIntegers() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<BigInteger> criteria = builder.createQuery(BigInteger.class);
    Root<Product> productRoot = criteria.from(Product.class);
    criteria.select(builder.sum(productRoot.get(Product_.someBigInteger)));
    Object sumResult = em.createQuery(criteria).getSingleResult();
    assertReturnType(BigInteger.class, sumResult);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) BigInteger(java.math.BigInteger) Product(org.hibernate.orm.test.jpa.metamodel.Product) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 7 with Product

use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.

the class AggregationResultTest method testSumOfBigDecimals.

/**
 * Sum of BigDecimal should return a BigDecimal
 */
@Test
public void testSumOfBigDecimals() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<BigDecimal> criteria = builder.createQuery(BigDecimal.class);
    Root<Product> productRoot = criteria.from(Product.class);
    criteria.select(builder.sum(productRoot.get(Product_.someBigDecimal)));
    Object sumResult = em.createQuery(criteria).getSingleResult();
    assertReturnType(BigDecimal.class, sumResult);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Product(org.hibernate.orm.test.jpa.metamodel.Product) BigDecimal(java.math.BigDecimal) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 8 with Product

use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.

the class AggregationResultTest method testSumOfFloats.

/**
 * Sum of Floats should return a Float; note that this is distinctly different than JPAQL
 */
@Test
public void testSumOfFloats() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Float> criteria = builder.createQuery(Float.class);
    Root<Product> productRoot = criteria.from(Product.class);
    criteria.select(builder.sum(productRoot.get(Product_.rating)));
    Object sumResult = em.createQuery(criteria).getSingleResult();
    assertReturnType(Float.class, sumResult);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Product(org.hibernate.orm.test.jpa.metamodel.Product) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 9 with Product

use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.

the class CastTest method testCastToString.

@Test
@SkipForDialect(value = DerbyDialect.class, comment = "Derby does not support cast from INTEGER to VARCHAR")
@TestForIssue(jiraKey = "HHH-5755")
public void testCastToString(EntityManagerFactoryScope scope) {
    scope.inTransaction(entityManager -> {
        Product product = new Product();
        product.setId("product1");
        product.setPrice(1.23d);
        product.setQuantity(QUANTITY);
        product.setPartNumber(((long) Integer.MAX_VALUE) + 1);
        product.setRating(1.999f);
        product.setSomeBigInteger(BigInteger.valueOf(987654321));
        product.setSomeBigDecimal(BigDecimal.valueOf(987654.321));
        entityManager.persist(product);
    });
    scope.inTransaction(entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
        Root<Product> root = criteria.from(Product.class);
        criteria.where(builder.equal(root.get(Product_.quantity).as(String.class), builder.literal(String.valueOf(QUANTITY))));
        List<Product> result = entityManager.createQuery(criteria).getResultList();
        Assertions.assertEquals(1, result.size());
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Product(org.hibernate.orm.test.jpa.metamodel.Product) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.jupiter.api.Test) AbstractCriteriaTest(org.hibernate.orm.test.jpa.criteria.AbstractCriteriaTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with Product

use of org.hibernate.orm.test.jpa.metamodel.Product in project hibernate-orm by hibernate.

the class ExpressionsTest method testParameterReuse.

@Test
public void testParameterReuse() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaQuery<Product> criteria = entityManager.getCriteriaBuilder().createQuery(Product.class);
        Root<Product> from = criteria.from(Product.class);
        ParameterExpression<String> param = entityManager.getCriteriaBuilder().parameter(String.class);
        Predicate predicate = entityManager.getCriteriaBuilder().equal(from.get(Product_.id), param);
        Predicate predicate2 = entityManager.getCriteriaBuilder().equal(from.get(Product_.name), param);
        criteria.where(entityManager.getCriteriaBuilder().or(predicate, predicate2));
        assertEquals(1, criteria.getParameters().size());
        TypedQuery<Product> query = entityManager.createQuery(criteria);
        int hqlParamCount = countGeneratedParameters(query.unwrap(Query.class));
        assertEquals(1, hqlParamCount);
        query.setParameter(param, "abc").getResultList();
    });
}
Also used : TypedQuery(jakarta.persistence.TypedQuery) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Query(org.hibernate.query.Query) Product(org.hibernate.orm.test.jpa.metamodel.Product) Predicate(jakarta.persistence.criteria.Predicate) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.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