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())));
Related
how I can do a query with Criteria API with between or between for this plain sql query:
SELECT * FROM
documents
WHERE (register_date BETWEEN to_date('02.07.2017', 'DD.MM.YYYY') AND to_date('02.07.2018', 'DD.MM.YYYY'))
OR (sign_date BETWEEN to_date('02.07.2017', 'DD.MM.YYYY') AND to_date('02.07.2018', 'DD.MM.YYYY'));
in postgres?
It's not a dublicate because I'm doing a query with TWO between and a question in the link below hasn't criteria api as I can see
I need to make something like this
CriteriaBuilder cb = this.getCSession().getCriteriaBuilder();
CriteriaQuery<Documents> documentsCriteria = cb.createQuery(model_class);
Root<Documents> DocumentsRoot = documentsCriteria.from(model_class);
documentsCriteria.select(DocumentsRoot);
List<Predicate> predicates = new ArrayList<>();
if (register_start_date != null && register_end_date != null) {
predicates.add(cb.between(DocumentsRoot.get("register_date"), register_start_date, register_end_date));
predicates.add(cb.between(DocumentsRoot.get("sign_date"), register_start_date, register_end_date));
}
documentsCriteria.where(cb.or(predicates.toArray(new Predicate[predicates.size()])));
List<Order> orderList = new ArrayList<>();
orderList.add(cb.asc(DocumentsRoot.get("register_date")));
orderList.add(cb.asc(DocumentsRoot.get("sign_date")));
documentsCriteria.orderBy(orderList);
Query q = this.getCSession().createQuery(documentsCriteria);
return (List<Documents>) q.getResultList();
you create the criteria based on the entity manager and then you add the between restriction to it.
Criteria criteria = entityManager.unwrap(Session.class).createCriteria(EntityDao.class);
criteria.add(Restrictions.between("your_date_column", date1, date2));
between or between
criteria.add(Restrictions.or(Restrictions.between("your_date_column", date1, date2), Restrictions.between("your_date_column", date3, date4)));
I need to search a text if exist using criteria builder.
If this text exist in any one column, I need to retrieve the data.
Sample Data:
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<Event> criteriaQuery = builder.createQuery(Event.class);
Root<Event> root = criteriaQuery.from(Event.class);
ArrayList<Predicate> conditions = new ArrayList<>();
conditions.add(builder.equal(root.get(Event_.eventId), searchText));
conditions.add(builder.equal(root.get(Event_.eventModule), searchText));
conditions.add(builder.equal(root.get(Event_.eventName), searchText));
criteriaQuery.select(root).where(builder.or(conditions.toArray(new Predicate[conditions.size()])));
javax.persistence.Query query = manager.createQuery(criteriaQuery);
List<Event> eventList = query.getResultList();
return eventList;
Right now I am using OR clause to check the data existence of data.
Can I know how can I change this query using where IN clause.
You don't need your OR condition:
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<Event> criteriaQuery = builder.createQuery(Event.class);
Root<Event> root = criteriaQuery.from(Event.class);
ArrayList<Predicate> conditions = new ArrayList<>();
conditions.add(builder.equal(root.get(Event_.eventId), searchText));
conditions.add(builder.equal(root.get(Event_.eventModule), searchText));
conditions.add(builder.equal(root.get(Event_.eventName), searchText));
//CHANGE THIS ROW
criteriaQuery.select(root).where(conditions.toArray(new Predicate[conditions.size()])));
javax.persistence.Query query = manager.createQuery(criteriaQuery);
List<Event> eventList = query.getResultList();
return eventList;
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();
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?
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();
}