Search in sources :

Example 36 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder 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 37 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder 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 38 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder 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 39 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class CriteriaLiteralInSelectExpressionTest method testNullLiteral.

@Test
@TestForIssue(jiraKey = "HHH-10861")
public void testNullLiteral() throws Exception {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        entityManager.getTransaction().begin();
        final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery(MyEntityDTO.class);
        final Root<MyEntity> entity = query.from(MyEntity.class);
        query.multiselect(criteriaBuilder.literal(false), criteriaBuilder.nullLiteral(String.class));
        final List<MyEntityDTO> dtos = entityManager.createQuery(query).getResultList();
        assertThat(dtos.size(), is(1));
        assertThat(dtos.get(0).active, is(false));
        assertThat(dtos.get(0).name, nullValue());
        assertThat(dtos.get(0).surname, nullValue());
        entityManager.getTransaction().commit();
    } catch (Exception e) {
        if (entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().rollback();
        }
        throw e;
    } finally {
        entityManager.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 40 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class CriteriaLiteralsTest method testLiteralsInSelectClause.

@Test
public void testLiteralsInSelectClause() throws Exception {
    try {
        doInJPA(this::entityManagerFactory, entityManager -> {
            final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<String> query = cb.createQuery(String.class);
            Root<Book> root = query.from(Book.class);
            ListJoin<Book, Author> authors = root.joinList("authors");
            query.where(cb.equal(root.get("name"), "Java Persistence with Hibernate")).select(cb.literal("( SELECT REPEAT('abc' || ' ', 10000000000 FROM MY_ENTITY )"));
            entityManager.createQuery(query).getResultList();
        });
    } catch (Exception expected) {
        assertEquals(QuerySyntaxException.class, expected.getCause().getClass());
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) QuerySyntaxException(org.hibernate.hql.internal.ast.QuerySyntaxException) QuerySyntaxException(org.hibernate.hql.internal.ast.QuerySyntaxException) SQLGrammarException(org.hibernate.exception.SQLGrammarException) Test(org.junit.Test)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2