Question

How to create criteria in hibernate for following query :

select * from xmppserveripinfo where update_dt > (select DATE_SUB(NOW(),INTERVAL 1 MINUTE))
Was it helpful?

Solution

This example demonstrates how it works:

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();
        }
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top