Search in sources :

Example 36 with Person

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

the class HQLTest method test_hql_api_list_example.

@Test
public void test_hql_api_list_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.name like :name").setParameter("name", "J%").list();
    });
}
Also used : Person(org.hibernate.userguide.model.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 37 with Person

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

the class HQLTest method test_hql_collection_expressions_example_5.

@Test
public void test_hql_collection_expressions_example_5() {
    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 = some elements ( 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 38 with Person

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

the class HQLTest method test_hql_select_simplest_example.

@Test
public void test_hql_select_simplest_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        List<Object> objects = session.createQuery("from java.lang.Object").list();
        List<Person> persons = session.createQuery("from Person").list();
    });
}
Also used : Person(org.hibernate.userguide.model.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 39 with Person

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

the class HQLTest method test_jpql_api_named_query_example.

@Test
public void test_jpql_api_named_query_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Query query = entityManager.createNamedQuery("get_person_by_name");
        TypedQuery<Person> typedQuery = entityManager.createNamedQuery("get_person_by_name", Person.class);
    });
}
Also used : TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 40 with Person

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

the class MySQLStoredProcedureTest method init.

@Before
public void init() {
    destroy();
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.doWork(connection -> {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("CREATE PROCEDURE sp_count_phones (" + "   IN personId INT, " + "   OUT phoneCount INT " + ") " + "BEGIN " + "    SELECT COUNT(*) INTO phoneCount " + "    FROM Phone p " + "    WHERE p.person_id = personId; " + "END");
                statement.executeUpdate("CREATE PROCEDURE sp_phones(IN personId INT) " + "BEGIN " + "    SELECT *  " + "    FROM Phone   " + "    WHERE person_id = personId;  " + "END");
                statement.executeUpdate("CREATE FUNCTION fn_count_phones(personId integer)  " + "RETURNS integer " + "DETERMINISTIC " + "READS SQL DATA " + "BEGIN " + "    DECLARE phoneCount integer; " + "    SELECT COUNT(*) INTO phoneCount " + "    FROM Phone p " + "    WHERE p.person_id = personId; " + "    RETURN phoneCount; " + "END");
            }
        });
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Person person1 = new Person("John Doe");
        person1.setNickName("JD");
        person1.setAddress("Earth");
        person1.setCreatedOn(Timestamp.from(LocalDateTime.of(2000, 1, 1, 0, 0, 0).toInstant(ZoneOffset.UTC)));
        person1.getAddresses().put(AddressType.HOME, "Home address");
        person1.getAddresses().put(AddressType.OFFICE, "Office address");
        entityManager.persist(person1);
        Phone phone1 = new Phone("123-456-7890");
        phone1.setId(1L);
        phone1.setType(PhoneType.MOBILE);
        person1.addPhone(phone1);
        Phone phone2 = new Phone("098_765-4321");
        phone2.setId(2L);
        phone2.setType(PhoneType.LAND_LINE);
        person1.addPhone(phone2);
    });
}
Also used : Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Phone(org.hibernate.userguide.model.Phone) Person(org.hibernate.userguide.model.Person) Session(org.hibernate.Session) Before(org.junit.Before)

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