use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testQuotientConversion.
@Test
@TestForIssue(jiraKey = "HHH-5803")
public void testQuotientConversion() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
Long longValue = 999999999L;
Path<Double> doublePath = orderRoot.get(Order_.totalPrice);
Path<Integer> integerPath = orderRoot.get(Order_.customer).get(Customer_.age);
orderCriteria.select(orderRoot);
Predicate p = builder.ge(builder.quot(integerPath, doublePath), longValue);
orderCriteria.where(p);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertTrue(orders.size() == 0);
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testComplicatedNotOr.
/**
* Check complicated not.
*/
@Test
public void testComplicatedNotOr() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
Predicate p1 = builder.equal(orderRoot.get("id"), "order-1");
Predicate p2 = builder.equal(orderRoot.get("id"), "order-2");
Predicate compoundPredicate = builder.not(builder.or(p1, p2));
// negated OR should become an AND
assertEquals(Predicate.BooleanOperator.AND, compoundPredicate.getOperator());
orderCriteria.where(compoundPredicate);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertEquals(1, orders.size());
Order order = orders.get(0);
assertEquals("order-3", order.getId());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class IdClassPredicateTest method testDeclaredIdClassAttributes.
@Test
public void testDeclaredIdClassAttributes() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
// Packaging arguments for use in query.
List<String> divisions = new ArrayList<String>();
divisions.add("NA");
divisions.add("EU");
// Building the query.
CriteriaBuilder criteria = em.getCriteriaBuilder();
CriteriaQuery<Widget> query = criteria.createQuery(Widget.class);
Root<Widget> root = query.from(Widget.class);
Predicate predicate = root.get("division").in(divisions);
query.where(predicate);
// Retrieving query.;
List<Widget> widgets = em.createQuery(query).getResultList();
Assert.assertEquals(4, widgets.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class IdClassPredicateTest method testSupertypeIdClassAttributes.
@Test
public void testSupertypeIdClassAttributes() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
// Packaging arguments for use in query.
List<String> types = new ArrayList<String>();
types.add("NA");
types.add("EU");
// Building the query.
CriteriaBuilder criteria = em.getCriteriaBuilder();
CriteriaQuery<Tool> query = criteria.createQuery(Tool.class);
Root<Tool> root = query.from(Tool.class);
Predicate predicate = root.get("type").in(types);
query.where(predicate);
// Retrieving query.
List<Tool> tools = em.createQuery(query).getResultList();
Assert.assertEquals(4, tools.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class SelectCaseTest method selectCaseWithCastedTypeValuesShouldWork.
@Test
public void selectCaseWithCastedTypeValuesShouldWork() {
EntityManager entityManager = getOrCreateEntityManager();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaBuilder.Case<String> selectCase = cb.selectCase();
Predicate somePredicate = cb.equal(cb.literal(1), 1);
selectCase.when(somePredicate, EnumValue.VALUE_1.name());
selectCase.otherwise(EnumValue.VALUE_2.name());
CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
Root<Entity> from = query.from(Entity.class);
query.select(from).where(cb.equal(from.get("value"), selectCase.as(String.class)));
entityManager.createQuery(query).getResultList();
}
Aggregations