如何使用Criteria API在此普通SQL查询之间或之间进行查询:
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'));
在postgres中?
这不是重复的,因为我正在进行两次查询,并且下面链接中的问题没有我所看到的标准api
我需要做这样的事情
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();
答案 0 :(得分:3)
您基于实体管理器创建条件,然后将其间的限制添加到其中。
render xlsx: 'latest_buttons', template: 'featured/latest'
之间或之间
Criteria criteria = entityManager.unwrap(Session.class).createCriteria(EntityDao.class);
criteria.add(Restrictions.between("your_date_column", date1, date2));