use of org.hibernate.orm.test.jpa.metamodel.Customer in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testIllegalArgumentExceptionBuildingTupleWithSameAliases.
@Test
public void testIllegalArgumentExceptionBuildingTupleWithSameAliases() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
final CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Customer> customerRoot = criteria.from(Customer.class);
Path<String> namePath = customerRoot.get(Customer_.name);
namePath.alias("age");
Path<Integer> agePath = customerRoot.get(Customer_.age);
agePath.alias("age");
try {
criteria.multiselect(namePath, agePath);
fail("Attempt to define multi-select with same aliases should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Customer 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.get("name"), "%Caruso"));
criteria.where(builder.exists(customerSubquery));
em.createQuery(criteria).getResultList();
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Customer 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();
}
use of org.hibernate.orm.test.jpa.metamodel.Customer in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testArray.
@Test
public void testArray() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
Customer c1 = new Customer();
c1.setId("c1");
c1.setAge(18);
c1.setName("Bob");
em.persist(c1);
em.getTransaction().commit();
em.close();
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
final CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Object[]> q = cb.createQuery(Object[].class);
Root<Customer> c = q.from(Customer.class);
q.select(cb.array(c.get(Customer_.name), c.get(Customer_.age)));
List<Object[]> result = em.createQuery(q).getResultList();
assertEquals(1, result.size());
assertEquals(c1.getName(), result.get(0)[0]);
assertEquals(c1.getAge(), result.get(0)[1]);
em.getTransaction().commit();
em.close();
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
em.createQuery("delete Customer").executeUpdate();
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Customer in project hibernate-orm by hibernate.
the class UncorrelatedSubqueryTest method testEqualAll.
@Test
public void testEqualAll() {
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)));
em.createQuery(criteria).getResultList();
em.getTransaction().commit();
em.close();
}
Aggregations