简体   繁体   English

Hibernate:将mysql Query Datedif转换为HQL查询

[英]Hibernate : convert mysql Query Datedif to HQL Query

here is my Query in mysql 这是我在mysql中的查询

SELECT DATEDIFF('2008-11-30','2008-11-29') AS DiffDate

how to convert in to Hibernate Query Language 如何转换为Hibernate查询语言

One of the way you can achieve this by extending mysql dialect and register it using registerFunction(String, SQLFunction) 通过扩展mysql方言并使用registerFunction(String, SQLFunction)注册它来实现此目的的方法之一

public class CustomMySQL5InnoDBDialect extends MySQL5InnoDBDialect {

  public CustomMySQL5InnoDBDialect () {
    super();
    registerFunction( "datediff", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "datediff(?1, ?2)" ) );
 }

}

I do not think that you could get this specific mysql function into JPA/HQL (you can still use some native query if you want). 我不认为你可以将这个特定的mysql函数放到JPA / HQL中(如果你愿意,你仍然可以使用一些本机查询)。 A better approach would be using a third party library like [Joda-time][1] to compute the number of days between two dates. 更好的方法是使用像[Joda-time][1]这样的第三方库来计算两个日期之间的天数。 I do not really see why we should use the database for this. 我真的不明白为什么我们应该使用数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM