use of jakarta.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class BasicCriteriaUsageTest method testDateCompositeCustomType.
@Test
@TestForIssue(jiraKey = "HHH-8283")
public void testDateCompositeCustomType() {
final Date date = Date.from(Instant.now());
final Payment payment = new Payment();
payment.setAmount(new BigDecimal(1000));
payment.setDate(date);
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(payment);
CriteriaQuery<Payment> criteria = em.getCriteriaBuilder().createQuery(Payment.class);
Root<Payment> rp = criteria.from(Payment.class);
Predicate predicate = em.getCriteriaBuilder().equal(rp.get(Payment_.date), date);
criteria.where(predicate);
TypedQuery<Payment> q = em.createQuery(criteria);
List<Payment> payments = q.getResultList();
assertEquals(1, payments.size());
em.getTransaction().commit();
em.close();
}
use of jakarta.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class SelectCaseLiteralHandlingBindTest method selectSumOnCaseExpression.
@Test
@TestForIssue(jiraKey = "HHH-13001")
public void selectSumOnCaseExpression() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> query = cb.createTupleQuery();
Root<Programmer> programmer = query.from(Programmer.class);
Predicate junior = cb.between(programmer.get("commits"), 0, 10);
Predicate senior = cb.between(programmer.get("commits"), 11, 20);
CriteriaBuilder.Case<Double> selectCase = cb.selectCase();
selectCase.when(junior, 1.1).when(senior, 2.2).otherwise(3.3);
query.multiselect(programmer.get("team"), cb.sum(selectCase)).groupBy(programmer.get("team")).orderBy(cb.asc(programmer.get("team")));
List<Tuple> resultList = entityManager.createQuery(query).getResultList();
assertNotNull(resultList);
assertTrue(resultList.isEmpty());
});
}
use of jakarta.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class GroupBySelectCaseTest method selectCaseInOrderByAndSelectExpression.
@Test
@TestForIssue(jiraKey = "HHH-12230")
public void selectCaseInOrderByAndSelectExpression() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> query = cb.createTupleQuery();
Root<Person> from = query.from(Person.class);
Predicate childPredicate = cb.between(from.get(Person_.AGE), 0, 10);
Predicate teenagerPredicate = cb.between(from.get(Person_.AGE), 11, 20);
CriteriaBuilder.Case<String> selectCase = cb.selectCase();
selectCase.when(childPredicate, "child").when(teenagerPredicate, "teenager").otherwise("adult");
query.multiselect(selectCase);
query.orderBy(cb.asc(selectCase));
List<Tuple> resultList = entityManager.createQuery(query).getResultList();
assertNotNull(resultList);
assertTrue(resultList.isEmpty());
});
}
use of jakarta.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();
Assertions.assertEquals(4, widgets.size());
em.getTransaction().commit();
em.close();
}
use of jakarta.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class PredicateTest method testCharArray.
/**
* Check predicate for field which has simple char array type (char[]).
*/
@Test
public void testCharArray() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
Predicate p = builder.equal(orderRoot.get("domen"), new char[] { 'r', 'u' });
orderCriteria.where(p);
List<Order> orders = em.createQuery(orderCriteria).getResultList();
assertTrue(orders.size() == 1);
em.getTransaction().commit();
em.close();
}
Aggregations