Search code examples
sql-serverhibernatejpql

How to see HQL final query


I'm trying to run the following query and getting a null pointer exception. I've verified that WHERE lockedTimestamp <= (GETDATE() - CAST('00:06' AS datetime) is valid msSQL because I can run it manually and it'll work fine. It appears HQL (Hibernate Query Language) may not like it. Any thoughts on how I can go about debugging this?

Query query = this.em.createQuery("SELECT c FROM Content c WHERE lockedTimestamp <= (GETDATE() - CAST('00:06' AS datetime))");

Solution

  • Why not use a much more obvious and intuitive calculation, e.g.

    SELECT c FROM Content c WHERE lockedTimestamp <= DATEADD(MINUTE, -6, GETDATE());
    

    ? I gather this isn't valid in JPQL either, but more for the general query itself in case other users stumble across this question...

    As an aside, why not put this query in a stored procedure? Surely hibernate can call a stored procedure, then it doesn't need to try to interpret or reverse engineer your query.