Search in sources :

Example 16 with Order

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

the class AbstractPathImplTest method testGetNonExistingAttributeViaName.

@ExpectedException(value = IllegalArgumentException.class)
@Test
public void testGetNonExistingAttributeViaName() {
    EntityManager em = getOrCreateEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
        Root<Order> orderRoot = criteria.from(Order.class);
        orderRoot.get("nonExistingAttribute");
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) 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) ExpectedException(org.hibernate.testing.orm.junit.ExpectedException)

Example 17 with Order

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

the class PredicateTest method prepareTestData.

@BeforeEach
public void prepareTestData() {
    builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(new Order("order-1", 1.0d));
    em.persist(new Order("order-2", 10.0d));
    em.persist(new Order("order-3", new char[] { 'r', 'u' }));
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with Order

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

the class PredicateTest method testEmptyInPredicate.

@Test
@TestForIssue(jiraKey = "HHH-8901")
public void testEmptyInPredicate() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    orderCriteria.select(orderRoot);
    orderCriteria.where(builder.in(orderRoot.get("totalPrice")));
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertTrue(orders.isEmpty());
    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) TestForIssue(org.hibernate.testing.TestForIssue)

Example 19 with Order

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

the class PredicateTest method testSimpleNot2.

/**
 * Check simple not.
 */
@Test
public void testSimpleNot2() {
    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.equal(orderRoot.get("id"), "order-1").not();
    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 20 with Order

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

the class PredicateTest method testEmptyDisjunction.

@Test
public void testEmptyDisjunction() {
    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.isFalse(builder.disjunction()));
    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