We are Borg
We are Borg

Reputation: 5311

Hibernate : HQL query to directly compare timestamp from database value

How is it possible for me to give an HQL query with Timestamp, something like if timestamp>oldTimestamp then delete that row. I have something like this till now :

 @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();
    }

What I would like to do is pass the query a parameter as use this as current timestamp denoting this is the currentTime, and delete all notes which are more than 5 minutes old. Any help would be nice. Thanks a lot.

Upvotes: 4

Views: 7875

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

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();

Upvotes: 3

Related Questions