use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class CorrelatedSubqueryTest method testRestrictedCorrelationNoExplicitSelection.
@Test
public void testRestrictedCorrelationNoExplicitSelection() {
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("customer");
customerSubquery.where(builder.like(orderCustomerJoin.<String>get("name"), "%Caruso"));
criteria.where(builder.exists(customerSubquery));
em.createQuery(criteria).getResultList();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager 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();
}
use of javax.persistence.EntityManager 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();
}
use of javax.persistence.EntityManager 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();
}
use of javax.persistence.EntityManager 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();
}
Aggregations