从表面上看,JPA Criteria Queries是Hibernate Criteria Queries的一大步。这是真的吗?

时间:2011-09-22 21:32:06

标签: hibernate jpa

这样一个巨大的退步怎么可能是合理的!?!我错过了什么吗?实体经理版本的可读性非常糟糕。

session()。createCriteria(Entity.class).add(Restrictions.eq(“id”),id).uniqueResult();

EntityManager em = getEntityManager();
try {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class);
    Metamodel m = em.getMetamodel();
    EntityType<Transaction> Transaction_ = m.entity(Transaction.class);
    Root<Transaction> transaction = cq.from(Transaction.class);

    // Error here. cannot find symbol. symbol: variable creationDate
    cq.where(cb.between(transaction.get(Transaction_.creationDate), startDate, endDate));

    // I also tried this:
    // cq.where(cb.between(Transaction_.getDeclaredSingularAttribute("creationDate"), startDate, endDate));

    List<Transaction> result = em.createQuery(cq).getResultList();
    return result;
} finally {
    em.close();
}

0 个答案:

没有答案