Search in sources :

Example 41 with CriteriaBuilder

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

the class CriteriaLiteralsTest method testLiteralsInWhereClause.

@Test
public void testLiteralsInWhereClause() throws Exception {
    doInJPA(this::entityManagerFactory, entityManager -> {
        final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        final CriteriaQuery<Tuple> query = cb.createQuery(Tuple.class);
        final Root<Book> entity = query.from(Book.class);
        query.where(cb.equal(entity.get("name"), cb.literal("( SELECT REPEAT('abc' || ' ', 10000000000 FROM MY_ENTITY )")));
        query.multiselect(cb.literal("abc"), entity.get("name"));
        connectionProvider.clear();
        List<Tuple> tuples = entityManager.createQuery(query).getResultList();
        assertEquals(1, connectionProvider.getPreparedStatements().size());
        assertNotNull(connectionProvider.getPreparedStatement("select 'abc' as col_0_0_, criteriali0_.name as col_1_0_ from Book criteriali0_ where criteriali0_.name=?"));
        assertTrue(tuples.isEmpty());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Tuple(javax.persistence.Tuple) Test(org.junit.Test)

Example 42 with CriteriaBuilder

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

the class CriteriaLiteralsTest method testCriteriaParameters.

@Test
public void testCriteriaParameters() throws Exception {
    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"), "( SELECT REPEAT('abc' || ' ', 10000000000 FROM MY_ENTITY )"), cb.equal(authors.index(), 0)).select(authors.get("name"));
        connectionProvider.clear();
        entityManager.createQuery(query).getResultList();
        assertEquals(1, connectionProvider.getPreparedStatements().size());
        assertNotNull(connectionProvider.getPreparedStatement("select authors1_.name as col_0_0_ from Book criteriali0_ inner join Author authors1_ on criteriali0_.id=authors1_.book_id where criteriali0_.name=? and authors1_.index_id=0"));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 43 with CriteriaBuilder

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

the class NullLiteralExpression method testNullLiteralExpressionInCriteriaUpdate.

@Test
@TestForIssue(jiraKey = "HHH-11159")
public void testNullLiteralExpressionInCriteriaUpdate() {
    EntityManager entityManager = getOrCreateEntityManager();
    try {
        entityManager.getTransaction().begin();
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaUpdate<Person> criteriaUpdate = builder.createCriteriaUpdate(Person.class);
        criteriaUpdate.from(Person.class);
        criteriaUpdate.set(Person_.subject, builder.nullLiteral(Subject.class));
        entityManager.createQuery(criteriaUpdate).executeUpdate();
        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 44 with CriteriaBuilder

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

the class AbstractPathImplTest method testIllegalDereference.

@Test
public void testIllegalDereference() {
    EntityManager em = getOrCreateEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
        Root<Order> orderRoot = criteria.from(Order.class);
        Path simplePath = orderRoot.get("totalPrice");
        // this should cause an ISE...
        try {
            simplePath.get("yabbadabbado");
            fail("Attempt to dereference basic path should throw IllegalStateException");
        } catch (IllegalStateException expected) {
        }
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) Path(javax.persistence.criteria.Path) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 45 with CriteriaBuilder

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

the class AbstractPathImplTest method testGetNonExistingAttributeViaName.

@Test(expected = IllegalArgumentException.class)
public void testGetNonExistingAttributeViaName() {
    EntityManager em = getOrCreateEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Order> criteria = criteriaBuilder.createQuery(Order.class);
        Root<Order> orderRoot = criteria.from(Order.class);
        orderRoot.get("nonExistingAttribute");
    } finally {
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

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