如何在hibernate中为日期特定查询创建条件

时间:2014-01-28 09:14:01

标签: hibernate hibernate-criteria

如何在hibernate中为以下查询创建条件:

select * from xmppserveripinfo where update_dt > (select DATE_SUB(NOW(),INTERVAL 1 MINUTE))

1 个答案:

答案 0 :(得分:1)

此示例演示了它的工作原理:

public List<XmppServerIpInfo > find(final int intervalInMinutes) {
    return getHibernateTemplate().execute(new HibernateCallback<List<XmppServerIpInfo>>() {

        public List<XmppServerIpInfo> doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria crit = session.createCriteria(XmppServerIpInfo.class);
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MINUTE, -intervalInMinutes);
            crit.add(Restrictions.ge("updateTime", cal.getTime()));
            crit.addOrder(Order.asc("updateTime"));
            return crit.list();
        }
    });
}