Hardik Lotiya Hardik Lotiya - 2 months ago 19
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

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)" ) );
 }

}
Comments