Search in sources :

Example 6 with Person

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

the class CriteriaTest method init.

@Before
public void init() {
    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);
        Person person2 = new Person("Mrs. John Doe");
        person2.setAddress("Earth");
        person2.setCreatedOn(Timestamp.from(LocalDateTime.of(2000, 1, 2, 12, 0, 0).toInstant(ZoneOffset.UTC)));
        entityManager.persist(person2);
        Person person3 = new Person("Dr_ John Doe");
        entityManager.persist(person3);
        Phone phone1 = new Phone("123-456-7890");
        phone1.setId(1L);
        phone1.setType(PhoneType.MOBILE);
        person1.addPhone(phone1);
        phone1.getRepairTimestamps().add(Timestamp.from(LocalDateTime.of(2005, 1, 1, 12, 0, 0).toInstant(ZoneOffset.UTC)));
        phone1.getRepairTimestamps().add(Timestamp.from(LocalDateTime.of(2006, 1, 1, 12, 0, 0).toInstant(ZoneOffset.UTC)));
        Call call11 = new Call();
        call11.setDuration(12);
        call11.setTimestamp(Timestamp.from(LocalDateTime.of(2000, 1, 1, 0, 0, 0).toInstant(ZoneOffset.UTC)));
        Call call12 = new Call();
        call12.setDuration(33);
        call12.setTimestamp(Timestamp.from(LocalDateTime.of(2000, 1, 1, 1, 0, 0).toInstant(ZoneOffset.UTC)));
        phone1.addCall(call11);
        phone1.addCall(call12);
        Phone phone2 = new Phone("098_765-4321");
        phone2.setId(2L);
        phone2.setType(PhoneType.LAND_LINE);
        Phone phone3 = new Phone("098-765-4320");
        phone3.setId(3L);
        phone3.setType(PhoneType.LAND_LINE);
        person2.addPhone(phone2);
        person2.addPhone(phone3);
        CreditCardPayment creditCardPayment = new CreditCardPayment();
        creditCardPayment.setCompleted(true);
        creditCardPayment.setAmount(BigDecimal.ZERO);
        creditCardPayment.setPerson(person1);
        WireTransferPayment wireTransferPayment = new WireTransferPayment();
        wireTransferPayment.setCompleted(true);
        wireTransferPayment.setAmount(BigDecimal.valueOf(100));
        wireTransferPayment.setPerson(person2);
        entityManager.persist(creditCardPayment);
        entityManager.persist(wireTransferPayment);
        Partner partner = new Partner("John Doe");
        entityManager.persist(partner);
    });
}
Also used : Call(org.hibernate.userguide.model.Call) CreditCardPayment(org.hibernate.userguide.model.CreditCardPayment) WireTransferPayment(org.hibernate.userguide.model.WireTransferPayment) Phone(org.hibernate.userguide.model.Phone) Person(org.hibernate.userguide.model.Person) Partner(org.hibernate.userguide.model.Partner) Before(org.junit.Before)

Example 7 with Person

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

the class HQLTest method test_hql_api_stream_example.

@Test
public void test_hql_api_stream_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        try (Stream<Person> persons = session.createQuery("select p " + "from Person p " + "where p.name like :name").setParameter("name", "J%").stream()) {
            Map<Phone, List<Call>> callRegistry = persons.flatMap(person -> person.getPhones().stream()).flatMap(phone -> phone.getCalls().stream()).collect(Collectors.groupingBy(Call::getPhone));
            process(callRegistry);
        }
    });
}
Also used : Call(org.hibernate.userguide.model.Call) Arrays(java.util.Arrays) H2Dialect(org.hibernate.dialect.H2Dialect) AddressType(org.hibernate.userguide.model.AddressType) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) Date(java.util.Date) LocalDateTime(java.time.LocalDateTime) Session(org.hibernate.Session) Person(org.hibernate.userguide.model.Person) FlushModeType(javax.persistence.FlushModeType) TypedQuery(javax.persistence.TypedQuery) CreditCardPayment(org.hibernate.userguide.model.CreditCardPayment) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ZoneOffset(java.time.ZoneOffset) StringType(org.hibernate.type.StringType) RequiresDialect(org.hibernate.testing.RequiresDialect) Before(org.junit.Before) WireTransferPayment(org.hibernate.userguide.model.WireTransferPayment) ScrollableResults(org.hibernate.ScrollableResults) TransactionUtil.doInJPA(org.hibernate.testing.transaction.TransactionUtil.doInJPA) TemporalType(javax.persistence.TemporalType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Timestamp(java.sql.Timestamp) Phone(org.hibernate.userguide.model.Phone) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PersonNames(org.hibernate.userguide.model.PersonNames) Collectors(java.util.stream.Collectors) CacheMode(org.hibernate.CacheMode) PhoneType(org.hibernate.userguide.model.PhoneType) List(java.util.List) Query(javax.persistence.Query) Stream(java.util.stream.Stream) Payment(org.hibernate.userguide.model.Payment) BaseEntityManagerFunctionalTestCase(org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase) MySQL5Dialect(org.hibernate.dialect.MySQL5Dialect) Assert.assertEquals(org.junit.Assert.assertEquals) Phone(org.hibernate.userguide.model.Phone) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.hibernate.userguide.model.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with Person

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

the class HQLTest method test_hql_api_scroll_projection_example.

@Test
public void test_hql_api_scroll_projection_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        try (ScrollableResults scrollableResults = session.createQuery("select p " + "from Person p " + "where p.name like :name").setParameter("name", "J%").scroll()) {
            while (scrollableResults.next()) {
                Person person = (Person) scrollableResults.get()[0];
                process(person);
            }
        }
    });
}
Also used : ScrollableResults(org.hibernate.ScrollableResults) Person(org.hibernate.userguide.model.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with Person

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

the class HQLTest method test_hql_api_unique_result_example.

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

Example 10 with Person

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

the class BatchTest method withScroll.

private void withScroll() {
    withBatch();
    //tag::batch-session-scroll-example[]
    EntityManager entityManager = null;
    EntityTransaction txn = null;
    ScrollableResults scrollableResults = null;
    try {
        entityManager = entityManagerFactory().createEntityManager();
        txn = entityManager.getTransaction();
        txn.begin();
        int batchSize = 25;
        Session session = entityManager.unwrap(Session.class);
        scrollableResults = session.createQuery("select p from Person p").setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
        int count = 0;
        while (scrollableResults.next()) {
            Person Person = (Person) scrollableResults.get(0);
            processPerson(Person);
            if (++count % batchSize == 0) {
                //flush a batch of updates and release memory:
                entityManager.flush();
                entityManager.clear();
            }
        }
        txn.commit();
    } catch (RuntimeException e) {
        if (txn != null && txn.isActive())
            txn.rollback();
        throw e;
    } finally {
        if (scrollableResults != null) {
            scrollableResults.close();
        }
        if (entityManager != null) {
            entityManager.close();
        }
    }
//end::batch-session-scroll-example[]
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ScrollableResults(org.hibernate.ScrollableResults) Person(org.hibernate.userguide.model.Person) StatelessSession(org.hibernate.StatelessSession) Session(org.hibernate.Session)

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