I want to select from database entities with certain price and with certain type but the result list is empty using criteria builder.I came to this code
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery();
Root<Advert> from = criteriaQuery.from(Advert.class);
Predicate predicate1 = criteriaBuilder.ge(from.get("price"), x1);
Predicate predicate2 = criteriaBuilder.le(from.get("price"), x2);
Predicate predicate4 = criteriaBuilder.like(from.get("type"), type);
criteriaQuery.where(criteriaBuilder.and(predicate1, predicate2, predicate4));
TypedQuery<Object> typedQuery = em.createQuery(criteriaQuery);
List<Object> resultList = typedQuery.getResultList();
But the it returns empty List.How to rewrite it correctly?
Related
I have the following java predicate based code that gets a result list from my MYSQL DB:
Root<Person> from = query.from(Person.class);
CriteriaQuery<Person> selectQuery = query.select(from);
List<Person> searchResults = new ArrayList<>();
Predicate jobPredicate = createPersonJobPredicate();
Predicate agePredicate = createPersonAgePredicate(); //currently not used
selectQuery = selectQuery.where(jobPredicate);
searchResults =entityManager.createQuery(selectQuery).setFirstResult(searchRequest.getIndex()).setMaxResults(searchRequest.getSize()).getResultList();
What I want to do is change the above code so that the result list has to match both predicates - e.g. must be job - "doctor" AND age - 45
I have tried combining the predicates as such below, but this always only returns the job predicate:
selectQuery = selectQuery.where(jobPredicate).where(agePredicate);
How can I do so?
Try something like:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class);
Root<Person> personRoot = criteriaQuery.from(Person.class);
Predicate jobPredicate = criteriaBuilder.equal(personRoot.get("job"), "doctor");
Predicate agePredicate = criteriaBuilder.greaterThan(personRoot.get("age"), 45);
Predicate combinedPredicate = criteriaBuilder.and(jobPredicate, agePredicate);
criteriaQuery.where(combinedPredicate);
List<Person> searchResults =
entityManager.createQuery(criteriaQuery).getResultList();
Code:
List<Predicate> conditionsList = new ArrayList<Predicate>();
Predicate onStart = criteriaBuilder.greaterThanOrEqualTo(criteriaRoot.get("insertDateTime"), startTradeDate);
Predicate onEnd = criteriaBuilder.lessThanOrEqualTo(criteriaRoot.get("insertDateTime"), endTradeDate);
conditionsList.add(onStart);
conditionsList.add(onEnd);
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<MyClass> criteriaQuery =
criteriaBuilder.createQuery(MyClass.class);
Root<MyClass> criteriaRoot = criteriaQuery.from(MyClass.class);
criteriaQuery.select(criteriaRoot).where(conditionsList);
The last line above doesn't compile because its expecting the conditionsList object to be a List of Boolean not list of Predicate.
Please advise me on the correct way of adding predicates above to the hibernate criteria?
Convert the List<Predicate> into an array Predicate[] like this:
criteriaQuery.select(criteriaRoot).where(conditionsList.toArray(new Predicate[] {}));
I need to get from DB items in a date range ("created"). Query is correct (i check it in HeidiSQL) and select right rows from DB, but typedQuery.resultList() always has 0 items.
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CurrencyConvertionRate> criteriaQuery = criteriaBuilder.createQuery(CurrencyConvertionRate.class);
Root<CurrencyConvertionRate> root = criteriaQuery.from(CurrencyConvertionRate.class);
List<Predicate> criteria = new ArrayList<Predicate>();
<Timestamp>get(created)={}", criteriaBuilder.greaterThanOrEqualTo(root.<Timestamp>get("created"), dateFrom).toString());
log.debug("criteriaBuilder.lessThanOrEqualTo(root.<Timestamp>get(created), dateTo={}", criteriaBuilder.lessThanOrEqualTo(root.<Timestamp>get("created"), dateTo).toString());
log.debug("root.get(currencyCode).in(currencyList)={}", root.get("currencyCode").in(currencyList).toString());
criteria.add(criteriaBuilder.greaterThanOrEqualTo(root.<Timestamp>get("created"), dateFrom));
criteria.add(criteriaBuilder.lessThanOrEqualTo(root.<Timestamp>get("created"), dateTo));
criteria.add(root.get("currencyCode").in(currencyList));
CriteriaQuery<CurrencyConvertionRate> select = criteriaQuery.select(root);
select.where(criteriaBuilder.and(criteria.toArray(new Predicate[0])));
TypedQuery<CurrencyConvertionRate> typedQuery = entityManager.createQuery(select);
return typedQuery.getResultList();
How can I make this select with JPA CriteriaBuilder like hibernate Criteria in my example?
Criteria criteria = getSession().createCriteria(Payment.class);
criteria.setFirstResult(0);
criteria.setMaxResults(1);
criteria.add(Restrictions.eq("statusStepOne", 0));
List list = criteria.list();
and
Criteria criteria = getSession().createCriteria(Payment.class);
criteria.add(Restrictions.eq("oid", oid));
criteria.add(Restrictions.between("dateCreated", Utils.getDateMinus(1), new Date()));
List list = criteria.list();
return (List<Payment>)list;
FIRST QUERY
Criteria criteria = getSession().createCriteria(Payment.class);
criteria.setFirstResult(0);
criteria.setMaxResults(1);
criteria.add(Restrictions.eq("statusStepOne", 0));
List list = criteria.list();
JPA FIRST QUERY
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Payment> query = cb.createQuery(Payment.class);
Root<Payment> p = query.from(Payment.class);
query.select(p);
query.where(cb.equal(p.get("statusStepOne"), 0));
query.setFirstResult(0);
query.setMaxResults(1);
List<Payment> list = query.getResultList();
SECOND QUERY
Criteria criteria = getSession().createCriteria(Payment.class);
criteria.add(Restrictions.eq("oid", oid));
criteria.add(Restrictions.between("dateCreated", Utils.getDateMinus(1), new Date()));
return (List<Payment>) criteria.list(); // I've removed useless asignment
JPA SECOND QUERY
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Payment> query = cb.createQuery(Payment.class);
Root<Payment> p = query.from(Payment.class);
query.select(p);
query.where(cb.equal(p.get("oid"), oid)
.and(cb.between(p.get("dateCreated"), Utils.getDateMinus(1), new Date())));
I have the following HQL statement:
select auditRecord from AuditRecordEntity auditRecord where auditRecord.auditAccount.accountId = :accountId
I'd like to convert it to use the javax.persistence.criteria.CriteriaBuilder, but am unsure what do do, any help is much appreciated!
CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Object> query = builder.createQuery();
Root<AuditRecordEntity> root = query.from(AuditRecordEntity.class);
// what next?
Try this:
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<AuditRecordEntity> criteriaQuery = criteriaBuilder.createQuery(AuditRecordEntity.class);
Root<AuditRecordEntity> root = criteriaQuery.from(AuditRecordEntity.class);
Predicate predicate = criteriaBuilder.equal(root.get("auditAccount").get("accountId"), accountId);
criteriaQuery.where(predicate);
TypedQuery<AuditRecordEntity> query = em.createQuery(criteriaQuery);
return query.getSingleResult();
you can do it like following
public List<UserProfile> getAuditRecords(String acountId) {
Criteria auditCriteria = session.createCriteria(AuditRecordEntity.class);
auditCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
auditCriteria.createCriteria("auditAccount").add(Restrictions.eq("accountId ",acountId));
return auditCriteria .list();
}