Search in sources :

Example 11 with Customer

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();
}
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) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 12 with Customer

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) Tuple(javax.persistence.Tuple) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 13 with Customer

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) Tuple(javax.persistence.Tuple) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 14 with Customer

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Customer(org.hibernate.jpa.test.metamodel.Customer) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 15 with Customer

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

Customer (org.hibernate.jpa.test.metamodel.Customer)25 Test (org.junit.Test)24 EntityManager (javax.persistence.EntityManager)21 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)21 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)19 Order (org.hibernate.jpa.test.metamodel.Order)7 Tuple (javax.persistence.Tuple)4 TestForIssue (org.hibernate.testing.TestForIssue)4 CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)3 CriteriaDelete (javax.persistence.criteria.CriteriaDelete)2 CriteriaUpdate (javax.persistence.criteria.CriteriaUpdate)2 Root (javax.persistence.criteria.Root)2 SkipForDialect (org.hibernate.testing.SkipForDialect)2 Query (javax.persistence.Query)1 CollectionJoin (javax.persistence.criteria.CollectionJoin)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Join (javax.persistence.criteria.Join)1 LineItem (org.hibernate.jpa.test.metamodel.LineItem)1 MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)1 RequiresDialect (org.hibernate.testing.RequiresDialect)1