2

我一直在尝试找到一种方法来查询我的表中修改日期超过 7 天的所有日期。

这是我在工作中的一些东西,但它不起作用:

    @SuppressWarnings({ "unchecked", "rawtypes" })
public List<Ownership> findOwnersByDate() {
    log.debug("finding a filtered list of Ownerships");
    Session session = getSessionFactory().getCurrentSession();
    try {
        session.beginTransaction();
        Criteria crit = session.createCriteria(Ownership.class)
        .add( Restrictions.gt("modDate", new Integer(7) ) ); 
        List results = crit.list();
        log.debug("get filtered ownerships successful, 
                    result size: " +  results.size());
        return results;
    } catch (RuntimeException re) {
        log.debug("query failed", re);
        log.error("get filtered ownerships failed", re);
        throw re;
    }
}
4

1 回答 1

3

尝试传递与当前日期减去 7 天相对应的 Date 对象,而不是传递new Integer(7)to 。Restrictions.gt()您可以为此使用 Calendar 类,例如:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -7);
...
Criteria crit = session.createCriteria(Ownership.class)
    .add( Restrictions.gt("modDate", cal.getTime() ) );
于 2013-03-28T20:00:16.723 回答