Search in sources :

Example 1 with Product

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();
}
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 2 with Product

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();
}
Also used : BigInteger(java.math.BigInteger) 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 3 with Product

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();
}
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 4 with Product

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

Example 5 with Product

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();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) 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