2016-04-29 5 views
0

この例では、JPA CriteriaBuilderのようにhibernate Criteriaのように選択できますか?JPA CriteriaBuilderは、批評を持つリストエンティティを取得します

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

最初のクエリ

Criteria criteria = getSession().createCriteria(Payment.class); 
    criteria.setFirstResult(0); 
    criteria.setMaxResults(1); 
    criteria.add(Restrictions.eq("statusStepOne", 0)); 
    List list = criteria.list(); 

JPA最初のクエリ

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()))); 
関連する問題