如何使用Timestamp提供HQL查询,例如if timestamp> oldTimestamp然后删除该行。到目前为止我有这样的事情:
@Override
@Scheduled(fixedRate = 100000)
public void removeStaleLocks() {
session = this.sessionFactory.getCurrentSession();
//Timestamp timestamp = // current timestamp;
Query query = session.createQuery("delete from NoteLock as nl where nl.timeStamp=:timeStamp");
query.setParameter("timeStamp",timeStamp);
query.executeUpdate();
session.flush();
}
我想要做的是将查询作为参数传递给当前时间戳,表示这是当前时间,并删除所有超过5分钟的注释。你能帮忙的话,我会很高兴。非常感谢。
答案 0 :(得分:3)
long now = System.currentTimeMillis();
long nowMinus5Minutes = now - (5L * 60L * 1000L);
Timestamp nowMinus5MinutesAsTimestamp = new Timestamp(nowMinus5Minutes);
Query query = session.createQuery("delete from NoteLock as nl where nl.timeStamp < :limit");
query.setParameter("limit", nowMinus5MinutesAsTimestamp);
query.executeUpdate();