Hardik Lotiya Hardik Lotiya - 1 year ago 107
Java Question

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

Answer Source

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 () {
    registerFunction( "datediff", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "datediff(?1, ?2)" ) );