问题
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
回答1:
try using GETDATE()
SELECT x FROM Bla x WHERE x.deadline < GETDATE()
回答2:
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.
来源:https://stackoverflow.com/questions/15298426/hibernate-jpql-current-date-not-working-on-sql-server