Search in sources :

Example 6 with Product

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

the class ExpressionsTest method prepareTestData.

@Before
public void prepareTestData() {
    builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    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));
    em.persist(product);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Before(org.junit.Before)

Example 7 with Product

use of org.hibernate.jpa.test.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() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    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));
    em.persist(product);
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder builder = em.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 = em.createQuery(criteria).getResultList();
    Assert.assertEquals(1, result.size());
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.createQuery("delete Product").executeUpdate();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 8 with Product

use of org.hibernate.jpa.test.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(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 9 with Product

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

the class AggregationResultTest method createTestData.

@Before
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(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Before(org.junit.Before)

Example 10 with Product

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

the class ExpressionsTest method testEmptyDisjunction.

@Test
public void testEmptyDisjunction() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    criteria.from(Product.class);
    criteria.where(builder.disjunction());
    List<Product> result = em.createQuery(criteria).getResultList();
    assertEquals(0, result.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

EntityManager (javax.persistence.EntityManager)22 Product (org.hibernate.jpa.test.metamodel.Product)22 Test (org.junit.Test)20 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)18 BigInteger (java.math.BigInteger)2 MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)2 CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Before (org.junit.Before)2 BigDecimal (java.math.BigDecimal)1 TypedQuery (javax.persistence.TypedQuery)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 Query (org.hibernate.Query)1 ComparisonPredicate (org.hibernate.query.criteria.internal.predicate.ComparisonPredicate)1 RequiresDialect (org.hibernate.testing.RequiresDialect)1 SkipForDialect (org.hibernate.testing.SkipForDialect)1