Search in sources :

Example 11 with Order

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

the class CriteriaCompilingTest method testDeprecation.

@Test
@TestForIssue(jiraKey = "HHH-10960")
public void testDeprecation() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        CriteriaBuilder builder = session.getCriteriaBuilder();
        CriteriaQuery<Order> query = builder.createQuery(Order.class);
        Root<Order> from = query.from(Order.class);
        query.orderBy(builder.desc(from.get("totalPrice")));
        TypedQuery<Order> jpaQuery = session.createQuery(query);
        Query<?> hibQuery = jpaQuery.unwrap(Query.class);
        ScrollableResults sr = hibQuery.scroll(ScrollMode.FORWARD_ONLY);
        hibQuery.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
        Query<Order> anotherQuery = session.createQuery("select o from Order o where totalPrice in :totalPrices", Order.class);
        anotherQuery.setParameterList("totalPrices", Arrays.asList(12.5d, 14.6d));
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 12 with Order

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

the class UncorrelatedSubqueryTest method testGetCorrelatedParentIllegalStateException.

@Test
public void testGetCorrelatedParentIllegalStateException() {
    // test that attempting to call getCorrelatedParent on a uncorrelated query/subquery
    // throws ISE
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
    Root<Customer> customerRoot = criteria.from(Customer.class);
    Join<Customer, Order> orderJoin = customerRoot.join(Customer_.orders);
    criteria.select(customerRoot);
    Subquery<Double> subCriteria = criteria.subquery(Double.class);
    Root<Order> subqueryOrderRoot = subCriteria.from(Order.class);
    subCriteria.select(builder.min(subqueryOrderRoot.get(Order_.totalPrice)));
    criteria.where(builder.equal(orderJoin.get("totalPrice"), builder.all(subCriteria)));
    assertFalse(customerRoot.isCorrelated());
    assertFalse(subqueryOrderRoot.isCorrelated());
    try {
        customerRoot.getCorrelationParent();
        fail("Should have resulted in IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        subqueryOrderRoot.getCorrelationParent();
        fail("Should have resulted in IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 13 with Order

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

the class CorrelatedSubqueryTest method testBasicCorrelation.

@Test
public void testBasicCorrelation() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
    Root<Customer> customer = criteria.from(Customer.class);
    criteria.select(customer);
    Subquery<Order> orderSubquery = criteria.subquery(Order.class);
    Root<Customer> customerCorrelationRoot = orderSubquery.correlate(customer);
    Join<Customer, Order> customerOrderCorrelationJoin = customerCorrelationRoot.join(Customer_.orders);
    orderSubquery.select(customerOrderCorrelationJoin);
    criteria.where(builder.not(builder.exists(orderSubquery)));
    em.createQuery(criteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 14 with Order

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

the class CorrelatedSubqueryTest method testRestrictedCorrelation.

@Test
public void testRestrictedCorrelation() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = criteria.from(Order.class);
    criteria.select(orderRoot);
    // create correlated subquery
    Subquery<Customer> customerSubquery = criteria.subquery(Customer.class);
    Root<Order> orderRootCorrelation = customerSubquery.correlate(orderRoot);
    Join<Order, Customer> orderCustomerJoin = orderRootCorrelation.join(Order_.customer);
    customerSubquery.where(builder.like(orderCustomerJoin.get(Customer_.name), "%Caruso")).select(orderCustomerJoin);
    criteria.where(builder.exists(customerSubquery));
    em.createQuery(criteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 15 with Order

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

the class CorrelatedSubqueryTest method testCorrelatedJoinsFromSubquery.

@Test
@TestForIssue(jiraKey = "HHH-8556")
public void testCorrelatedJoinsFromSubquery() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    CriteriaQuery<Customer> cquery = builder.createQuery(Customer.class);
    Root<Customer> customer = cquery.from(Customer.class);
    cquery.select(customer);
    Subquery<Order> sq = cquery.subquery(Order.class);
    Join<Customer, Order> sqo = sq.correlate(customer.join(Customer_.orders));
    sq.select(sqo);
    Set<Join<?, ?>> cJoins = sq.getCorrelatedJoins();
    // ensure the join is returned in #getCorrelatedJoins
    assertEquals(cJoins.size(), 1);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) Join(jakarta.persistence.criteria.Join) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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