简体   繁体   中英

Hibernate : convert mysql Query Datedif to HQL Query

here is my Query in mysql

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

how to convert in to Hibernate Query Language

One of the way you can achieve this by extending mysql dialect and register it using 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). A better approach would be using a third party library like [Joda-time][1] to compute the number of days between two dates. I do not really see why we should use the database for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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