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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations