Search in sources :

Example 1 with Address

use of org.hibernate.jpa.test.metamodel.Address in project hibernate-orm by hibernate.

the class QueryBuilderTest method testEqualityComparisonEntityConversion.

@Test
public void testEqualityComparisonEntityConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Address address = new Address("Street Id", "Fake Street", "Fake City", "Fake State", "Fake Zip");
    Phone phone1 = new Phone("1", "555", "0001", address);
    Phone phone2 = new Phone("2", "555", "0002", address);
    Phone phone3 = new Phone("3", "555", "0003", address);
    Phone phone4 = new Phone("4", "555", "0004");
    List<Phone> phones = new ArrayList<Phone>(3);
    phones.add(phone1);
    phones.add(phone2);
    phones.add(phone3);
    address.setPhones(phones);
    em.persist(address);
    em.persist(phone4);
    em.getTransaction().commit();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    EntityType<Phone> Phone_ = mm.entity(Phone.class);
    CriteriaQuery<Phone> cquery = cb.createQuery(Phone.class);
    Root<Phone> phone = cquery.from(Phone.class);
    ComparisonPredicate predicate = (ComparisonPredicate) cb.equal(phone.get(Phone_.getSingularAttribute("address", Address.class)), address);
    cquery.where(predicate);
    List<Phone> results = em.createQuery(cquery).getResultList();
    assertEquals(3, results.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) Address(org.hibernate.jpa.test.metamodel.Address) Phone(org.hibernate.jpa.test.metamodel.Phone) ArrayList(java.util.ArrayList) ComparisonPredicate(org.hibernate.query.criteria.internal.predicate.ComparisonPredicate) Test(org.junit.Test)

Example 2 with Address

use of org.hibernate.jpa.test.metamodel.Address in project hibernate-orm by hibernate.

the class ListIndexTest method testListIndex.

@Test
@TestForIssue(jiraKey = "HHH-8404")
public void testListIndex() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Address address1 = new Address();
    address1.setId("a1");
    Phone phone1 = new Phone();
    phone1.setId("p1");
    phone1.setAddress(address1);
    Phone phone2 = new Phone();
    phone2.setId("p2");
    phone2.setAddress(address1);
    address1.getPhones().add(phone1);
    address1.getPhones().add(phone2);
    Address address2 = new Address();
    address2.setId("a2");
    Phone phone3 = new Phone();
    phone3.setId("p3");
    phone3.setAddress(address2);
    address2.getPhones().add(phone3);
    em.persist(phone1);
    em.persist(phone2);
    em.persist(phone3);
    em.persist(address1);
    em.persist(address2);
    em.getTransaction().commit();
    em.clear();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Address> criteria = cb.createQuery(Address.class);
    Root<Address> addressRoot = criteria.from(Address.class);
    ListJoin<Address, Phone> phones = addressRoot.join(Address_.phones);
    criteria.where(cb.gt(phones.index(), 0));
    List<Address> results = em.createQuery(criteria).getResultList();
    assertNotNull(results);
    // Ensure that the "index(phones) > 0" condition was included on the inner join, meaning only address1
    // (> 1 phone) was returned.
    assertEquals(1, results.size());
    assertEquals(address1.getId(), results.get(0).getId());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Address(org.hibernate.jpa.test.metamodel.Address) Phone(org.hibernate.jpa.test.metamodel.Phone) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with Address

use of org.hibernate.jpa.test.metamodel.Address in project hibernate-orm by hibernate.

the class PluralAttributeExpressionsTest method testCollectionIsEmptyCriteria.

@Test
public void testCollectionIsEmptyCriteria() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        final HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder();
        final CriteriaQuery<Address> criteria = cb.createQuery(Address.class);
        final Root<Address> root = criteria.from(Address.class);
        criteria.select(root).where(cb.isEmpty(root.get(Address_.phones)));
        entityManager.createQuery(criteria).getResultList();
    });
}
Also used : Address(org.hibernate.jpa.test.metamodel.Address) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 4 with Address

use of org.hibernate.jpa.test.metamodel.Address in project hibernate-orm by hibernate.

the class PluralAttributeExpressionsTest method testCollectionSizeCriteria.

@Test
public void testCollectionSizeCriteria() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        final HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder();
        final CriteriaQuery<Address> criteria = cb.createQuery(Address.class);
        final Root<Address> root = criteria.from(Address.class);
        criteria.select(root).where(cb.gt(cb.size(root.get(Address_.phones)), 1));
        entityManager.createQuery(criteria).getResultList();
    });
}
Also used : Address(org.hibernate.jpa.test.metamodel.Address) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

Address (org.hibernate.jpa.test.metamodel.Address)4 Test (org.junit.Test)4 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)3 EntityManager (javax.persistence.EntityManager)2 Phone (org.hibernate.jpa.test.metamodel.Phone)2 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)2 ArrayList (java.util.ArrayList)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)1 CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)1 ComparisonPredicate (org.hibernate.query.criteria.internal.predicate.ComparisonPredicate)1 TestForIssue (org.hibernate.testing.TestForIssue)1