use of javax.persistence.criteria.Predicate 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());
});
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class ExpressionsTest method testParameterReuse.
@Test
public void testParameterReuse() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Product> criteria = em.getCriteriaBuilder().createQuery(Product.class);
Root<Product> from = criteria.from(Product.class);
ParameterExpression<String> param = em.getCriteriaBuilder().parameter(String.class);
Predicate predicate = em.getCriteriaBuilder().equal(from.get(Product_.id), param);
Predicate predicate2 = em.getCriteriaBuilder().equal(from.get(Product_.name), param);
criteria.where(em.getCriteriaBuilder().or(predicate, predicate2));
assertEquals(1, criteria.getParameters().size());
TypedQuery<Product> query = em.createQuery(criteria);
int hqlParamCount = countGeneratedParameters(query.unwrap(Query.class));
assertEquals(1, hqlParamCount);
query.setParameter(param, "abc").getResultList();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class BasicCriteriaUsageTest method testDateCompositeCustomType.
@Test
@TestForIssue(jiraKey = "HHH-8283")
public void testDateCompositeCustomType() {
Payment payment = new Payment();
payment.setAmount(new BigDecimal(1000));
payment.setDate(new Date());
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(payment);
CriteriaQuery<Payment> criteria = em.getCriteriaBuilder().createQuery(Payment.class);
Root<Payment> rp = criteria.from(Payment.class);
Predicate predicate = em.getCriteriaBuilder().equal(rp.get(Payment_.date), new Date());
criteria.where(predicate);
TypedQuery<Payment> q = em.createQuery(criteria);
List<Payment> payments = q.getResultList();
assertEquals(1, payments.size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class BasicCriteriaUsageTest method testParameterCollection.
@Test
public void testParameterCollection() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Wall> criteria = em.getCriteriaBuilder().createQuery(Wall.class);
Root<Wall> from = criteria.from(Wall.class);
ParameterExpression param = em.getCriteriaBuilder().parameter(String.class);
SingularAttribute<? super Wall, ?> colorAttribute = em.getMetamodel().entity(Wall.class).getDeclaredSingularAttribute("color");
assertNotNull("metamodel returned null singular attribute", colorAttribute);
Predicate predicate = em.getCriteriaBuilder().equal(from.get(colorAttribute), param);
criteria.where(predicate);
assertEquals(1, criteria.getParameters().size());
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.
the class SelectCaseTest method selectCaseWithValuesShouldWork.
@Test
public void selectCaseWithValuesShouldWork() {
EntityManager entityManager = getOrCreateEntityManager();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaBuilder.Case<EnumValue> selectCase = cb.selectCase();
Predicate somePredicate = cb.equal(cb.literal(1), 1);
selectCase.when(somePredicate, EnumValue.VALUE_1);
selectCase.otherwise(EnumValue.VALUE_2);
CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
Root<Entity> from = query.from(Entity.class);
query.select(from).where(cb.equal(from.get("value"), selectCase));
entityManager.createQuery(query).getResultList();
}
Aggregations