We have an entity column with a formula property that requires today’s date at midnight in order to calculate itself property:
DATEADD(DD, DATEDIFF(DD, 0, GETDATE()), 0)
Hibernate transforms this into:
DATEADD(mytablename0_.DD, DATEDIFF(mytablename0_.DD, 0, GETDATE()), 0)
I’ve double checked that my dialect is SqlServer. I’ve also replaced DD with DAY, but the problem remains.
How do I get hibernate to recognize that DD is a keyword?
I had the same issue when upgrading from Sybase to SQLServer. The field in my scenario was an integer field used to store a date since 1970-01-01. The DATEADD function was used to convert that integer into a genuine date so we had a good representation through the hibernate mapped objects.
My original mapping for this field was the following:
This resulted in the problem that you described in the question, where the resulting sql passed to the database was:
Updating the mapping to surround the dateadd keyword dd in double quotes resolved this issue for me.
SQL Server 2008 accepted the generated SQL below and executed as expected.