Search in sources :

Example 1 with LineItem

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

the class OnKeywordTest method basicTest.

@Test
public void basicTest() {
    EntityManager em = getOrCreateEntityManager();
    CriteriaQuery<Order> criteria = em.getCriteriaBuilder().createQuery(Order.class);
    Root<Order> root = criteria.from(Order.class);
    criteria.select(root);
    CollectionJoin<Order, LineItem> lineItemsJoin = root.join(Order_.lineItems);
    lineItemsJoin.on(em.getCriteriaBuilder().gt(lineItemsJoin.get(LineItem_.quantity), em.getCriteriaBuilder().literal(20)));
    em.createQuery(criteria).getResultList();
    em.close();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) LineItem(org.hibernate.jpa.test.metamodel.LineItem) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 2 with LineItem

use of org.hibernate.jpa.test.metamodel.LineItem 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 3 with LineItem

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

the class ImplicitJoinTest method testImplicitJoinFromExplicitCollectionJoin.

@Test
public void testImplicitJoinFromExplicitCollectionJoin() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
    Root<Order> orderRoot = criteria.from(Order.class);
    Join<Order, LineItem> lineItemsJoin = orderRoot.join(Order_.lineItems);
    criteria.where(criteriaBuilder.lt(lineItemsJoin.get(LineItem_.quantity), 2));
    criteria.select(orderRoot).distinct(true);
    TypedQuery<Order> query = em.createQuery(criteria);
    query.getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) LineItem(org.hibernate.jpa.test.metamodel.LineItem) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

EntityManager (javax.persistence.EntityManager)3 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)3 LineItem (org.hibernate.jpa.test.metamodel.LineItem)3 Order (org.hibernate.jpa.test.metamodel.Order)3 Test (org.junit.Test)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Customer (org.hibernate.jpa.test.metamodel.Customer)1 SkipForDialect (org.hibernate.testing.SkipForDialect)1