Search in sources :

Example 71 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class ExpressionsTest method testSumWithQuotient.

@Test
public void testSumWithQuotient() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Number> criteria = builder.createQuery(Number.class);
    criteria.from(Product.class);
    criteria.select(builder.quot(builder.sum(builder.literal(BigDecimal.valueOf(0.0)), builder.literal(BigDecimal.valueOf(1.0))), BigDecimal.valueOf(2.0)));
    Number result = em.createQuery(criteria).getSingleResult();
    assertEquals(0.5d, result.doubleValue(), 0.1d);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 72 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class ExpressionsTest method testDiffWithQuotient.

@Test
public void testDiffWithQuotient() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Number> criteria = builder.createQuery(Number.class);
    criteria.from(Product.class);
    criteria.select(builder.quot(builder.diff(builder.literal(BigDecimal.valueOf(2.0)), builder.literal(BigDecimal.valueOf(1.0))), BigDecimal.valueOf(2.0)));
    Number result = em.createQuery(criteria).getSingleResult();
    assertEquals(0.5d, result.doubleValue(), 0.1d);
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 73 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class ExpressionsTest method testParameterReuse.

@Test
public void testParameterReuse() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = em.getCriteriaBuilder().createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    ParameterExpression<String> param = em.getCriteriaBuilder().parameter(String.class);
    Predicate predicate = em.getCriteriaBuilder().equal(from.get(Product_.id), param);
    Predicate predicate2 = em.getCriteriaBuilder().equal(from.get(Product_.name), param);
    criteria.where(em.getCriteriaBuilder().or(predicate, predicate2));
    assertEquals(1, criteria.getParameters().size());
    TypedQuery<Product> query = em.createQuery(criteria);
    int hqlParamCount = countGeneratedParameters(query.unwrap(Query.class));
    assertEquals(1, hqlParamCount);
    query.setParameter(param, "abc").getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) TypedQuery(javax.persistence.TypedQuery) Query(org.hibernate.Query) Product(org.hibernate.jpa.test.metamodel.Product) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 74 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class ExpressionsTest method testInExplicitTupleList.

@Test
public void testInExplicitTupleList() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.where(from.get(Product_.partNumber).in(Collections.singletonList(((long) Integer.MAX_VALUE) + 1)));
    List<Product> result = em.createQuery(criteria).getResultList();
    assertEquals(1, 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)

Example 75 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class ExpressionsTest method testEmptyInList.

@Test
@TestForIssue(jiraKey = "HHH-6876")
@RequiresDialect(H2Dialect.class)
public void testEmptyInList() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    // empty IN list
    criteria.where(from.get(Product_.partNumber).in());
    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) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Aggregations

EntityManager (javax.persistence.EntityManager)1435 Test (org.junit.Test)879 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)211 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)75 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)59 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 PersistenceException (javax.persistence.PersistenceException)43 ArrayList (java.util.ArrayList)41 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)38 Item (org.hibernate.jpa.test.Item)36 Session (org.hibernate.Session)35