2013-03-28 76 views
1

我一直在試圖找到一種方法來查詢我的表中的所有日期,其中修改日期早於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; 
    } 
} 
+0

嘗試把日期/日曆對象的'GT()',而不是一個整數,無論​​使用何種類型的'modDate'是。 –

回答

3

傳遞new Integer(7)Restrictions.gt()相反的,嘗試通過對應於當前日期減去7天的Date對象。您可以使用日曆類是,例如:

Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.DATE, -7); 
... 
Criteria crit = session.createCriteria(Ownership.class) 
    .add(Restrictions.gt("modDate", cal.getTime()));