Hibernate Dialects + datediff function

风流意气都作罢 提交于 2019-12-01 09:09:40

问题


I have a problem in that the syntax for datediff in mysql is different from that in hsqldb:

mysql: datediff(date1,date2)
hsqldb: datediff(interval,date1,date2)

The dialects in hibernate usually resolve these issues, however I don't seem to be able to find a way of creating a datediff restriction for hibernate. This is a real nuisance because it prevents me from unit testing with an in-memory hsql database since I have to 'hardcode' the format of datediff in a sql statement.

If anyone has advice on this matter it would be greatly appreciated.


回答1:


I would say that the easiest solution is to create your own custom HSQLDB dialect, extending the existing HSQLDB dialect. Then, in the constructor, register a function to handle datediff(date1, date2) to be translated into datediff(interval, date1, date2), provided that interval would be a static value. Something like this:

registerFunction( "datediff", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "datediff(interval, ?1, ?2)" ) );


来源:https://stackoverflow.com/questions/4606807/hibernate-dialects-datediff-function

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