问题描述
我有一个问题,那就是mysql中用于datediff的语法与hsqldb中的不同:
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)
休眠通常可以解决这些问题,但我似乎无法找到一种为休眠创建datediff限制的方法。这是一个真正的麻烦,因为它阻止了我使用内存中的hsql数据库进行单元测试,因为我必须在sql语句中对datediff格式进行硬编码。
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.
推荐答案
我会说最简单的解决方案是创建自己的定制HSQLDB方言,扩展现有的HSQLDB方言。然后,在构造函数中,注册一个函数来处理datediff(date1,date2),将其转换为datediff(interval,date1,date2),前提是间隔应该是一个静态值。像这样:
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)" ) );
这篇关于Hibernate Dialects + datediff功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!