Hibernate JPQL CURRENT_DATE not working on SQL Server

风流意气都作罢 提交于 2019-12-07 15:20:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!