Search in sources :

Example 1 with Predicate

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();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Predicate(jakarta.persistence.criteria.Predicate) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with Predicate

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());
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Tuple(jakarta.persistence.Tuple) Predicate(jakarta.persistence.criteria.Predicate) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with Predicate

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());
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Tuple(jakarta.persistence.Tuple) Predicate(jakarta.persistence.criteria.Predicate) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with Predicate

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();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) ArrayList(java.util.ArrayList) Predicate(jakarta.persistence.criteria.Predicate) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 5 with Predicate

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();
}
Also used : Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Predicate(jakarta.persistence.criteria.Predicate) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Aggregations

Predicate (jakarta.persistence.criteria.Predicate)75 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)56 EntityManager (jakarta.persistence.EntityManager)41 Test (org.junit.jupiter.api.Test)21 Test (org.junit.Test)19 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)15 ArrayList (java.util.ArrayList)11 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)11 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)10 Expression (jakarta.persistence.criteria.Expression)9 TestForIssue (org.hibernate.testing.TestForIssue)9 Order (org.hibernate.orm.test.jpa.metamodel.Order)8 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)7 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)6 Tuple (jakarta.persistence.Tuple)5 ParameterExpression (jakarta.persistence.criteria.ParameterExpression)5 Vector (java.util.Vector)5 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)4 Date (java.util.Date)4 Expression (org.eclipse.persistence.expressions.Expression)4