use of org.hibernate.query.criteria.internal.predicate.ComparisonPredicate 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.query.criteria.internal.predicate.ComparisonPredicate in project hibernate-orm by hibernate.
the class QueryBuilderTest method testEqualityComparisonLiteralConversion.
@Test
public void testEqualityComparisonLiteralConversion() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
CriteriaQuery<Integer> cquery = cb.createQuery(Integer.class);
Root<Product> product = cquery.from(Product.class);
EntityType<Product> Product_ = mm.entity(Product.class);
cquery.select(cb.toInteger(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
ComparisonPredicate predicate = (ComparisonPredicate) cb.equal(product.get(Product_.getSingularAttribute("partNumber", Long.class)), 373767373);
assertEquals(Long.class, predicate.getRightHandOperand().getJavaType());
cquery.where(predicate);
em.createQuery(cquery).getResultList();
predicate = (ComparisonPredicate) cb.ge(cb.length(product.get(Product_.getSingularAttribute("name", String.class))), 4L);
assertEquals(Integer.class, predicate.getRightHandOperand().getJavaType());
cquery.where(predicate);
em.createQuery(cquery).getResultList();
em.getTransaction().commit();
em.close();
}
Aggregations