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();
}
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());
}
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();
});
}
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();
});
}
Aggregations