Hibernate 公式注解——MySql 函数:INTERVAL、DAY [英] Hibernate Formula Annotation - MySql functions: INTERVAL, DAY
本文介绍了Hibernate 公式注解——MySql 函数:INTERVAL、DAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有代码:
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Formula(value = "(SELECT count(history.city_id) FROM history where history.ts > (now() - INTERVAL 30 DAY) and history.city_id = id)")
private int last30daysUpdates;
所以,hiberante 将这个公式解析为:
so, hiberante parse this formula to:
...where
history.ts > (
now() - entitycity0_.INTERVAL 30 entitycity0_.DAY
) ...
错误是:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '30 entitycity0_.DAY) 附近使用的正确语法
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '30 entitycity0_.DAY)
我怎么能说休眠了 INTERVAL 和 DAY 是 MysqL 的功能?可能吗?
How can i say hibernate that INTERVAL and DAY are the functions of MysqL? Is it possible?
谢谢.
推荐答案
您使用哪种 MySQL 方言?如果 MySqlDialect 或 MySql5Dialect 你可以使用如下:
Which MySQL dialect are you use? If MySqlDialect oder MySql5Dialect you can use follows:
SELECT count(history.city_id) FROM history where timediff(now(), history.ts) < '720' and history.city_id = id
或者定义新的休眠函数
public class ExtendedMySQL5Dialect extends MySQL5Dialect
{
public ExtendedMySQL5Dialect()
{
super();
registerFunction( "date_sub_interval", new SQLFunctionTemplate( Hibernate.DATE, "date_sub(?1, INTERVAL ?2 ?3)" ) );
registerFunction( "date_add_interval", new SQLFunctionTemplate( Hibernate.DATE, "date_add(?1, INTERVAL ?2 ?3)" ) );
}
}
查询:
History.ts < date_sub_interval(now(), 30, DAY)
这篇关于Hibernate 公式注解——MySql 函数:INTERVAL、DAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文