mercredi 1 avril 2015

Hibernate : HQL query to directly compare timestamp from database value


  • I am working on a Spring-MVC application using Hibernate as ORM and PostgreSQL database, in which I am saving in a database row some values(obvious). With that information, I am also saving the TimeStamp(java.sql.TimeStamp) when the entry was made. For some reasons I want to remove entries which are older than 5 minutes.


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.


Aucun commentaire:

Enregistrer un commentaire