Search in sources :

Example 96 with CriteriaBuilder

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

the class MapJoinTestWithEmbeddable method testSelectingKeyOfMapJoin.

@Test
@TestForIssue(jiraKey = "HHH-10455")
public void testSelectingKeyOfMapJoin() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Node> query = cb.createQuery(Node.class);
        Root<Batch> root = query.from(Batch.class);
        MapJoin nodes = (MapJoin) root.join("batchNodeMetadata");
        query.select(nodes.key());
        query.where(cb.equal(root.get("id"), 1));
        entityManager.createQuery(query).getResultList();
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MapJoin(javax.persistence.criteria.MapJoin) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 97 with CriteriaBuilder

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

the class MapJoinTestWithEmbeddable method testSelectingValueOfMapJoin.

@Test
@TestForIssue(jiraKey = "HHH-10229")
public void testSelectingValueOfMapJoin() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Node> query = cb.createQuery(Node.class);
        Root<Batch> root = query.from(Batch.class);
        MapJoin nodes = (MapJoin) root.join("batchNodeMetadata");
        query.select(nodes);
        query.where(cb.equal(root.get("id"), 1));
        entityManager.createQuery(query).getResultList();
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MapJoin(javax.persistence.criteria.MapJoin) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 98 with CriteriaBuilder

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

the class CriteriaLiteralInSelectExpressionTest method testNullLiteralFirst.

@Test
@TestForIssue(jiraKey = "HHH-10861")
public void testNullLiteralFirst() 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.nullLiteral(String.class), entity.get("surname"));
        final List<MyEntityDTO> dtos = entityManager.createQuery(query).getResultList();
        assertThat(dtos.size(), is(1));
        assertThat(dtos.get(0).name, nullValue());
        assertThat(dtos.get(0).surname, is("A"));
        assertThat(dtos.get(0).active, is(false));
        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 99 with CriteriaBuilder

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

the class CriteriaLiteralInSelectExpressionTest method testStringLiteral2.

@Test
@TestForIssue(jiraKey = "HHH-9021")
@SkipForDialect(value = { Oracle8iDialect.class, DB2Dialect.class, SQLServerDialect.class, SybaseDialect.class })
public void testStringLiteral2() {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<Tuple> criteriaQuery = builder.createQuery(Tuple.class);
        criteriaQuery.from(MyEntity.class);
        criteriaQuery.multiselect(builder.equal(builder.literal(1), builder.literal(2)));
        final TypedQuery<Tuple> typedQuery = entityManager.createQuery(criteriaQuery);
        final List<Tuple> results = typedQuery.getResultList();
        assertThat(results.size(), is(1));
        assertThat(results.get(0).getElements().size(), is(1));
        assertThat(results.get(0).get(0), is(false));
    } finally {
        entityManager.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 100 with CriteriaBuilder

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

the class SelectCaseTest method selectCaseWithCastedTypeValuesShouldWork.

@Test
public void selectCaseWithCastedTypeValuesShouldWork() {
    EntityManager entityManager = getOrCreateEntityManager();
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaBuilder.Case<String> selectCase = cb.selectCase();
    Predicate somePredicate = cb.equal(cb.literal(1), 1);
    selectCase.when(somePredicate, EnumValue.VALUE_1.name());
    selectCase.otherwise(EnumValue.VALUE_2.name());
    CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
    Root<Entity> from = query.from(Entity.class);
    query.select(from).where(cb.equal(from.get("value"), selectCase.as(String.class)));
    entityManager.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) 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