Search in sources :

Example 1 with Order

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

the class AbstractPathImplTest method testIllegalDereference.

@Test
public void testIllegalDereference() {
    EntityManager em = getOrCreateEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
        Root<Order> orderRoot = criteria.from(Order.class);
        Path simplePath = orderRoot.get("totalPrice");
        // this should cause an ISE...
        try {
            simplePath.get("yabbadabbado");
            fail("Attempt to dereference basic path should throw IllegalStateException");
        } catch (IllegalStateException expected) {
        }
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) Path(jakarta.persistence.criteria.Path) EntityManager(jakarta.persistence.EntityManager) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 2 with Order

use of org.hibernate.orm.test.jpa.metamodel.Order 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)

Example 3 with Order

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

the class PredicateTest method testSimpleNot.

/**
 * Check simple not.
 */
@Test
public void testSimpleNot() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    orderCriteria.select(orderRoot);
    final Predicate p = builder.not(builder.equal(orderRoot.get("id"), "order-1"));
    assertEquals(Predicate.BooleanOperator.AND, p.getOperator());
    orderCriteria.where(p);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertEquals(2, 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 4 with Order

use of org.hibernate.orm.test.jpa.metamodel.Order 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();
}
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 5 with Order

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

the class PredicateTest method testEmptyConjunction.

@Test
public void testEmptyConjunction() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // yes this is a retarded case, but explicitly allowed in the JPA spec
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    orderCriteria.select(orderRoot);
    orderCriteria.where(builder.isTrue(builder.conjunction()));
    em.createQuery(orderCriteria).getResultList();
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertTrue(orders.size() == 3);
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) 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