Search in sources :

Example 6 with CriteriaBuilder

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

the class CriteriaTest method test_criteria_typedquery_multiselect_explicit_array_example.

@Test
public void test_criteria_typedquery_multiselect_explicit_array_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Object[]> criteria = builder.createQuery(Object[].class);
        Root<Person> root = criteria.from(Person.class);
        Path<Long> idPath = root.get(Person_.id);
        Path<String> nickNamePath = root.get(Person_.nickName);
        criteria.select(builder.array(idPath, nickNamePath));
        criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
        List<Object[]> idAndNickNames = entityManager.createQuery(criteria).getResultList();
        assertEquals(1, idAndNickNames.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 7 with CriteriaBuilder

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

the class CriteriaTest method test_criteria_param_example.

@Test
public void test_criteria_param_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
        Root<Person> root = criteria.from(Person.class);
        ParameterExpression<String> nickNameParameter = builder.parameter(String.class);
        criteria.where(builder.equal(root.get(Person_.nickName), nickNameParameter));
        TypedQuery<Person> query = entityManager.createQuery(criteria);
        query.setParameter(nickNameParameter, "JD");
        List<Person> persons = query.getResultList();
        assertEquals(1, persons.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 8 with CriteriaBuilder

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

the class CriteriaTest method test_criteria_from_root_example.

@Test
public void test_criteria_from_root_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
        Root<Person> root = criteria.from(Person.class);
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 9 with CriteriaBuilder

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

the class QueryBuilderTest method testMissingDialectFunction.

@Test
@TestForIssue(jiraKey = "HHH-10737")
@FailureExpected(jiraKey = "HHH-10737")
public void testMissingDialectFunction() {
    doInJPA(this::entityManagerFactory, em -> {
        Human human = new Human();
        human.setId(200L);
        human.setName("2");
        human.setBorn(new Date());
        em.persist(human);
        em.getTransaction().commit();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<HumanDTO> criteria = cb.createQuery(HumanDTO.class);
        Root<Human> root = criteria.from(Human.class);
        criteria.select(cb.construct(HumanDTO.class, root.get(Human_.id), root.get(Human_.name), cb.function("convert", String.class, root.get(Human_.born), cb.literal(110))));
        em.createQuery(criteria).getResultList();
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Date(java.util.Date) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with CriteriaBuilder

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

the class TreatJoinTest method testTreatJoin2.

@Test
@TestForIssue(jiraKey = "HHH-8488")
public void testTreatJoin2() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Bid> query = cb.createQuery(Bid.class);
        Root<Bid> bid = query.from(Bid.class);
        cb.treat(bid.join("item"), Book.class);
        cb.treat(bid.join("item"), Car.class);
        query.select(bid);
        final List<Bid> resultList = entityManager.createQuery(query).getResultList();
        assertThat(resultList.size(), is(2));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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