Search in sources :

Example 1 with Partner

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

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

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

the class SQLTest 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);
        Captain captain = new Captain();
        captain.setId(new Identity());
        captain.getId().setFirstname("Jean-Luc");
        captain.getId().setLastname("Picard");
        entityManager.persist(captain);
        SpaceShip spaceShip = new SpaceShip();
        spaceShip.setName("Enterprise");
        spaceShip.setDimensions(new Dimensions());
        spaceShip.getDimensions().setLength(100);
        spaceShip.getDimensions().setWidth(20);
        spaceShip.setModel("E-1");
        spaceShip.setSpeed(150);
        spaceShip.setCaptain(captain);
        entityManager.persist(spaceShip);
    });
}
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 4 with Partner

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

the class SQLTest method test_sql_hibernate_multi_entity_query_alias_example.

@Test
public void test_sql_hibernate_multi_entity_query_alias_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        List<Object> entities = session.createSQLQuery("SELECT {pr.*}, {pt.*} " + "FROM Person pr, Partner pt " + "WHERE pr.name = pt.name").addEntity("pr", Person.class).addEntity("pt", Partner.class).list();
        assertEquals(1, entities.size());
    });
}
Also used : Partner(org.hibernate.userguide.model.Partner) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Partner (org.hibernate.userguide.model.Partner)4 Person (org.hibernate.userguide.model.Person)3 Call (org.hibernate.userguide.model.Call)2 CreditCardPayment (org.hibernate.userguide.model.CreditCardPayment)2 Phone (org.hibernate.userguide.model.Phone)2 WireTransferPayment (org.hibernate.userguide.model.WireTransferPayment)2 Before (org.junit.Before)2 Test (org.junit.Test)2 Tuple (javax.persistence.Tuple)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 Session (org.hibernate.Session)1