Search in sources :

Example 6 with Customer

use of org.hibernate.jpa.test.metamodel.Customer 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(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) Customer(org.hibernate.jpa.test.metamodel.Customer) Join(javax.persistence.criteria.Join) CollectionJoin(javax.persistence.criteria.CollectionJoin) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 7 with Customer

use of org.hibernate.jpa.test.metamodel.Customer 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(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 8 with Customer

use of org.hibernate.jpa.test.metamodel.Customer 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(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 9 with Customer

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

the class CorrelatedSubqueryTest method testCorrelationExplicitSelectionCorrelation.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-3032")
public void testCorrelationExplicitSelectionCorrelation() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Customer> customerCriteria = builder.createQuery(Customer.class);
    Root<Customer> customer = customerCriteria.from(Customer.class);
    Join<Customer, Order> o = customer.join(Customer_.orders);
    Subquery<Order> sq = customerCriteria.subquery(Order.class);
    Join<Customer, Order> sqo = sq.correlate(o);
    Join<Order, LineItem> sql = sqo.join(Order_.lineItems);
    sq.where(builder.gt(sql.get(LineItem_.quantity), 3));
    // use the correlation itself as the subquery selection (initially caused problems wrt aliases)
    sq.select(sqo);
    customerCriteria.select(customer).distinct(true);
    customerCriteria.where(builder.exists(sq));
    em.createQuery(customerCriteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) LineItem(org.hibernate.jpa.test.metamodel.LineItem) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 10 with Customer

use of org.hibernate.jpa.test.metamodel.Customer 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(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

Customer (org.hibernate.jpa.test.metamodel.Customer)25 Test (org.junit.Test)24 EntityManager (javax.persistence.EntityManager)21 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)21 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)19 Order (org.hibernate.jpa.test.metamodel.Order)7 Tuple (javax.persistence.Tuple)4 TestForIssue (org.hibernate.testing.TestForIssue)4 CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)3 CriteriaDelete (javax.persistence.criteria.CriteriaDelete)2 CriteriaUpdate (javax.persistence.criteria.CriteriaUpdate)2 Root (javax.persistence.criteria.Root)2 SkipForDialect (org.hibernate.testing.SkipForDialect)2 Query (javax.persistence.Query)1 CollectionJoin (javax.persistence.criteria.CollectionJoin)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Join (javax.persistence.criteria.Join)1 LineItem (org.hibernate.jpa.test.metamodel.LineItem)1 MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)1 RequiresDialect (org.hibernate.testing.RequiresDialect)1