Search in sources :

Example 26 with Person

use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.

the class CriteriaTest method test_criteria_typedquery_expression_example.

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

Example 27 with Person

use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.

the class CriteriaTest method test_criteria_group_by_example.

@Test
public void test_criteria_group_by_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
        Root<Person> root = criteria.from(Person.class);
        criteria.groupBy(root.get("address"));
        criteria.multiselect(root.get("address"), builder.count(root));
        List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
        for (Tuple tuple : tuples) {
            String name = (String) tuple.get(0);
            Long count = (Long) tuple.get(1);
        }
        assertEquals(2, tuples.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Tuple(javax.persistence.Tuple) Test(org.junit.Test)

Example 28 with Person

use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.

the class CriteriaTest method test_criteria_typedquery_entity_example.

@Test
public void test_criteria_typedquery_entity_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
        Root<Person> root = criteria.from(Person.class);
        criteria.select(root);
        criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
        List<Person> persons = entityManager.createQuery(criteria).getResultList();
        assertEquals(1, persons.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 29 with Person

use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.

the class CriteriaTest method test_criteria_from_join_example.

@Test
public void test_criteria_from_join_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Phone> criteria = builder.createQuery(Phone.class);
        Root<Phone> root = criteria.from(Phone.class);
        Join<Phone, Person> personJoin = root.join(Phone_.person);
        Join<Person, String> addressesJoin = personJoin.join(Person_.addresses);
        criteria.where(builder.isNotEmpty(root.get(Phone_.calls)));
        List<Phone> phones = entityManager.createQuery(criteria).getResultList();
        assertEquals(2, phones.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Phone(org.hibernate.userguide.model.Phone) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 30 with Person

use of org.hibernate.userguide.model.Person in project hibernate-orm by hibernate.

the class CriteriaTest method test_criteria_tuple_example.

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

Aggregations

Person (org.hibernate.userguide.model.Person)40 Test (org.junit.Test)31 Session (org.hibernate.Session)16 Phone (org.hibernate.userguide.model.Phone)15 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)12 Call (org.hibernate.userguide.model.Call)6 Before (org.junit.Before)6 EntityTransaction (javax.persistence.EntityTransaction)4 ScrollableResults (org.hibernate.ScrollableResults)4 CreditCardPayment (org.hibernate.userguide.model.CreditCardPayment)4 WireTransferPayment (org.hibernate.userguide.model.WireTransferPayment)4 EntityManager (javax.persistence.EntityManager)3 Query (javax.persistence.Query)3 Tuple (javax.persistence.Tuple)3 TypedQuery (javax.persistence.TypedQuery)3 StatelessSession (org.hibernate.StatelessSession)3 RequiresDialect (org.hibernate.testing.RequiresDialect)3 Partner (org.hibernate.userguide.model.Partner)3 Statement (java.sql.Statement)2 BigDecimal (java.math.BigDecimal)1