Search in sources :

Example 1 with Product

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

the class QueryBuilderTest method testTypeConversion.

@Test
public void testTypeConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) 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 : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test)

Example 2 with Product

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

the class ExpressionsTest method testEmptyConjunctionIsFalse.

@Test
public void testEmptyConjunctionIsFalse() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    criteria.from(Product.class);
    criteria.where(builder.isFalse(builder.and()));
    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)

Example 3 with Product

use of org.hibernate.jpa.test.metamodel.Product 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 4 with Product

use of org.hibernate.jpa.test.metamodel.Product 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 5 with Product

use of org.hibernate.jpa.test.metamodel.Product 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)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