Search in sources :

Example 16 with Customer

use of org.hibernate.jpa.test.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();
}
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)

Example 17 with Customer

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

the class TupleCriteriaTest method testIllegalArgumentExceptionBuildingSelectArrayWithSameAliases.

@Test
public void testIllegalArgumentExceptionBuildingSelectArrayWithSameAliases() {
    EntityManager em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    final CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery criteria = builder.createQuery();
    Root<Customer> customerRoot = criteria.from(Customer.class);
    Path<String> namePath = customerRoot.get(Customer_.name);
    Path<Integer> agePath = customerRoot.get(Customer_.age);
    try {
        CompoundSelection<Object[]> c = builder.array(namePath.alias("SAME"), agePath.alias("SAME"));
        criteria.select(c);
        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) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 18 with Customer

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

the class TupleCriteriaTest method testInvalidTupleIndexAccess.

@Test
public void testInvalidTupleIndexAccess() {
    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();
    // the actual assertion block
    em = entityManagerFactory().createEntityManager();
    em.getTransaction().begin();
    final CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
    Root<Customer> customerRoot = criteria.from(Customer.class);
    criteria.multiselect(customerRoot.get(Customer_.name), customerRoot.get(Customer_.age));
    List<Tuple> results = em.createQuery(criteria).getResultList();
    assertEquals(1, results.size());
    Tuple tuple = results.get(0);
    try {
        tuple.get(99);
        fail("99 is invalid index");
    } catch (IllegalArgumentException expected) {
    }
    try {
        tuple.get(99, String.class);
        fail("99 is invalid index");
    } catch (IllegalArgumentException expected) {
    }
    tuple.get(0, String.class);
    tuple.get(1, Integer.class);
    try {
        tuple.get(0, java.util.Date.class);
        fail("Date is invalid type");
    } 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 19 with Customer

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

the class CriteriaCompilingTest method setUp.

@Before
public void setUp() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        Customer customer = new Customer();
        customer.setId("id");
        customer.setName(" David R. Vincent ");
        entityManager.persist(customer);
        customer = new Customer();
        customer.setId("id2");
        customer.setName("R Vincent");
        entityManager.persist(customer);
    });
}
Also used : Customer(org.hibernate.jpa.test.metamodel.Customer) Before(org.junit.Before)

Example 20 with Customer

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

the class CriteriaCompilingTest method testTrim.

@Test
public void testTrim() {
    final String expectedResult = "David R. Vincent";
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<String> cquery = cb.createQuery(String.class);
        Root<Customer> cust = cquery.from(Customer.class);
        EntityType<Customer> Customer_ = cust.getModel();
        cquery.where(cb.equal(cust.get(Customer_.getSingularAttribute("name", String.class)), cb.literal(" David R. Vincent ")));
        cquery.select(cb.trim(CriteriaBuilder.Trimspec.BOTH, cust.get(Customer_.getSingularAttribute("name", String.class))));
        TypedQuery<String> tq = entityManager.createQuery(cquery);
        String result = tq.getSingleResult();
        Assert.assertEquals("Mismatch in received results", expectedResult, result);
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test)

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