Search in sources :

Example 1 with Person

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

the class HQLTest method test_hql_collection_expressions_example_4.

@Test
public void test_hql_collection_expressions_example_4() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Call call = entityManager.createQuery("select c from Call c", Call.class).getResultList().get(0);
        Phone phone = call.getPhone();
        List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where :phone member of p.phones", Person.class).setParameter("phone", phone).getResultList();
        assertEquals(1, persons.size());
    });
}
Also used : Call(org.hibernate.userguide.model.Call) Phone(org.hibernate.userguide.model.Phone) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 2 with Person

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

the class CriteriaTest method test_criteria_from_multiple_root_example.

@Test
public void test_criteria_from_multiple_root_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        String address = "Earth";
        String prefix = "J%";
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
        Root<Person> personRoot = criteria.from(Person.class);
        Root<Partner> partnerRoot = criteria.from(Partner.class);
        criteria.multiselect(personRoot, partnerRoot);
        Predicate personRestriction = builder.and(builder.equal(personRoot.get(Person_.address), address), builder.isNotEmpty(personRoot.get(Person_.phones)));
        Predicate partnerRestriction = builder.and(builder.like(partnerRoot.get(Partner_.name), prefix), builder.equal(partnerRoot.get(Partner_.version), 0));
        criteria.where(builder.and(personRestriction, partnerRestriction));
        List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
        assertEquals(2, tuples.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Partner(org.hibernate.userguide.model.Partner) Tuple(javax.persistence.Tuple) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Example 3 with Person

use of org.hibernate.userguide.model.Person 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 4 with Person

use of org.hibernate.userguide.model.Person 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 5 with Person

use of org.hibernate.userguide.model.Person 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)

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