Search in sources :

Example 21 with Order

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

the class PredicateTest method testNotMultipleOr.

/**
 * Check complicated not.
 */
@Test
public void testNotMultipleOr() {
    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 p3 = builder.equal(orderRoot.get("id"), "order-3");
    final Predicate compoundPredicate = builder.or(p1, p2, p3).not();
    // negated OR should become an AND
    assertEquals(Predicate.BooleanOperator.AND, compoundPredicate.getOperator());
    orderCriteria.where(compoundPredicate);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertEquals(0, orders.size());
    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)

Example 22 with Order

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

the class PredicateTest method testExplicitBuilderBooleanHandling.

@Test
public void testExplicitBuilderBooleanHandling() {
    // just checking syntax of the resulting query
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // note : these may fail on various matrix db jobs depending on how the dialect handles booleans
    {
        CriteriaQuery<CreditCard> criteriaQuery = builder.createQuery(CreditCard.class);
        Root<CreditCard> root = criteriaQuery.from(CreditCard.class);
        criteriaQuery.where(builder.isFalse(root.get(CreditCard_.approved)));
        em.createQuery(criteriaQuery).getResultList();
    }
    {
        CriteriaQuery<Order> criteriaQuery = builder.createQuery(Order.class);
        Root<Order> root = criteriaQuery.from(Order.class);
        criteriaQuery.where(builder.isFalse(root.get(Order_.creditCard).get(CreditCard_.approved)));
        em.createQuery(criteriaQuery).getResultList();
    }
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Root(jakarta.persistence.criteria.Root) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) CreditCard(org.hibernate.orm.test.jpa.metamodel.CreditCard) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 23 with Order

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

the class PredicateTest method testComplicatedNotAnd.

/**
 * Check complicated not.
 */
@Test
public void testComplicatedNotAnd() {
    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.and(p1, p2).not();
    orderCriteria.where(compoundPredicate);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertEquals(3, orders.size());
    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

Order (org.hibernate.orm.test.jpa.metamodel.Order)23 EntityManager (jakarta.persistence.EntityManager)21 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)21 Test (org.junit.jupiter.api.Test)21 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)10 Predicate (jakarta.persistence.criteria.Predicate)8 Customer (org.hibernate.orm.test.jpa.metamodel.Customer)7 TestForIssue (org.hibernate.testing.TestForIssue)4 SkipForDialect (org.hibernate.testing.orm.junit.SkipForDialect)3 JiraKey (org.hibernate.testing.orm.junit.JiraKey)2 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 Join (jakarta.persistence.criteria.Join)1 Path (jakarta.persistence.criteria.Path)1 Root (jakarta.persistence.criteria.Root)1 ScrollableResults (org.hibernate.ScrollableResults)1 Session (org.hibernate.Session)1 CreditCard (org.hibernate.orm.test.jpa.metamodel.CreditCard)1 LineItem (org.hibernate.orm.test.jpa.metamodel.LineItem)1 ExpectedException (org.hibernate.testing.orm.junit.ExpectedException)1 Test (org.junit.Test)1