Search in sources :

Example 1 with LineItem

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

the class CorrelatedSubqueryTest method testCorrelationExplicitSelectionCorrelation.

@Test
@JiraKey("HHH-3032")
@SkipForDialect(dialectClass = SybaseASEDialect.class, majorVersion = 15)
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(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) LineItem(org.hibernate.orm.test.jpa.metamodel.LineItem) SkipForDialect(org.hibernate.testing.orm.junit.SkipForDialect) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Aggregations

EntityManager (jakarta.persistence.EntityManager)1 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)1 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)1 Customer (org.hibernate.orm.test.jpa.metamodel.Customer)1 LineItem (org.hibernate.orm.test.jpa.metamodel.LineItem)1 Order (org.hibernate.orm.test.jpa.metamodel.Order)1 JiraKey (org.hibernate.testing.orm.junit.JiraKey)1 SkipForDialect (org.hibernate.testing.orm.junit.SkipForDialect)1 Test (org.junit.jupiter.api.Test)1