0

here is my Query in mysql

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

how to convert in to Hibernate Query Language

0

2 Answers 2

2

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

create above class and update your hibernate configuaration with CustomMySQL5InnoDBDialect instead of MySQL5InnoDBDialect
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.