Florian Huonder
Florian Huonder

Reputation: 461

Hibernate JPQL CURRENT_DATE not working on SQL Server

I have the following JPQL query:

SELECT x FROM Bla x WHERE x.deadline < CURRENT_DATE

This works as expected on MySQL. But SQL Server complains that CURRENT_DATE is not recognized.

I am asking myself what exactly the problem is. Then CURRENT_DATE is a standard JPA function that should resolve independent from the underlying RDBMS.

Further the Hibernate documentation also has CURRENT_DATE documented (see here: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html#ql-expressions).

I googled and found a lot fo comments telling me that I shall use CURRENT_DATE() instead. But this isn't JPQL.

So why is CURRENT_DATE not working and what is the solution?

I am using Hibernate 4.1.8.Final.

Best regards, Florian

Upvotes: 4

Views: 2392

Answers (2)

Nayan Wadekar
Nayan Wadekar

Reputation: 11602

You can try to pass current date as a parameter to the query. Also, there will no portability issue whith this.

session.createQuery("SELECT x FROM TABLE_NAME x WHERE x.deadline < :currentDate")
        .setParameter( "currentDate", currentDate)
        .list();

Else, you can try query with current_date() instead of current_date, which is part of HQL expression.

Upvotes: 2

John Woo
John Woo

Reputation: 263733

try using GETDATE()

SELECT x FROM Bla x WHERE x.deadline < GETDATE()

Upvotes: 0

Related Questions