开发者

Hibernate Dialects + datediff function

开发者 https://www.devze.com 2023-02-03 04:57 出处:网络
I have a problem in that the syntax for datediff in mysql is different from that in hsqldb: mysql: datediff(date1,date2)

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 i开发者_运维技巧t would be greatly appreciated.


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)" ) );
0

精彩评论

暂无评论...
验证码 换一张
取 消