use of org.hibernate.jpa.test.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();
}
use of org.hibernate.jpa.test.metamodel.Customer in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testVariousTupleAccessMethods.
@Test
public void testVariousTupleAccessMethods() {
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 builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Customer> customerRoot = criteria.from(Customer.class);
Path<String> namePath = customerRoot.get(Customer_.name);
namePath.alias("NAME");
Path<Integer> agePath = customerRoot.get(Customer_.age);
agePath.alias("AGE");
criteria.multiselect(namePath, agePath);
List<Tuple> results = em.createQuery(criteria).getResultList();
Tuple tuple = results.get(0);
assertNotNull(tuple);
assertNotNull(tuple.get("NAME"));
assertNotNull(tuple.get("NAME", String.class));
try {
tuple.get("NAME", Date.class);
fail("Accessing Customer#name tuple as Date should have thrown exception");
} catch (IllegalArgumentException expected) {
}
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.jpa.test.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.jpa.test.metamodel.Customer in project hibernate-orm by hibernate.
the class TupleCriteriaTest method testTuple.
@Test
public void testTuple() {
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 builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Customer> customerRoot = criteria.from(Customer.class);
Path<String> namePath = customerRoot.get(Customer_.name);
Path<Integer> agePath = customerRoot.get(Customer_.age);
agePath.alias("age");
criteria.multiselect(namePath, agePath);
List<Tuple> results = em.createQuery(criteria).getResultList();
assertEquals(1, results.size());
Object resultElement = results.get(0);
assertTrue("Check result 'row' as Tuple", Tuple.class.isInstance(resultElement));
Tuple resultElementTuple = (Tuple) resultElement;
Object[] tupleArray = resultElementTuple.toArray();
assertEquals(2, tupleArray.length);
assertEquals(tupleArray[0], resultElementTuple.get(0));
assertEquals(resultElementTuple.get(namePath), resultElementTuple.get(0));
assertEquals(tupleArray[1], resultElementTuple.get(1));
assertEquals(resultElementTuple.get(agePath), resultElementTuple.get(1));
assertEquals(resultElementTuple.get(agePath), resultElementTuple.get("age"));
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.jpa.test.metamodel.Customer in project hibernate-orm by hibernate.
the class UncorrelatedSubqueryTest method testLessThan.
@Test
public void testLessThan() {
CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Customer> criteria = builder.createQuery(Customer.class);
Root<Customer> customerRoot = criteria.from(Customer.class);
Subquery<Double> subCriteria = criteria.subquery(Double.class);
Root<Customer> subQueryCustomerRoot = subCriteria.from(Customer.class);
subCriteria.select(builder.avg(subQueryCustomerRoot.get(Customer_.age)));
criteria.where(builder.lessThan(customerRoot.get(Customer_.age), subCriteria.getSelection().as(Integer.class)));
em.createQuery(criteria).getResultList();
em.getTransaction().commit();
em.close();
}
Aggregations