如何在我的示例中使用JPA CriteriaBuilder进行选择,如hibernate Criteria?
Criteria criteria = getSession().createCriteria(Payment.class);
criteria.setFirstResult(0);
criteria.setMaxResults(1);
criteria.add(Restrictions.eq("statusStepOne", 0));
List list = criteria.list();
和
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;
答案 0 :(得分:1)
第一次查询
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();
第二次查询
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())));