开发者

How to conver date type in hibernate criteria

I have a situation. I have two kinds of date in the mysql database. One is date and another is datetime. Now in hibernate criteria I have to check whether one date is greater than the other or not?

criteria.add(Restrictions.lt("award.deadline", "submission.date_received"));

But the different types are causing problems showing "java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date".

Even I tried to parse it 开发者_StackOverflow社区using date parser but it is not taking as date as it is taking as string only. So, can you tell me how can we convert one date to different type inside the hibernate criteria?


The problem is, that Restrictions.lt(String propertyName, Object value) is the wrong Restriction. What you need is Restrictions.ltProperty(String propertyName, String otherPropertyName).

Explanation:

  • Restrictions.lt(String propertyName, Object value) is to compare a entity property with a specific Value
  • Restrictions.ltProperty(String propertyName, String otherPropertyName) is to compare a two entity properties

If you use Restrictions.lt("Y", "X") and "Y" is the name of a date property, then hibernate would try to translate "X" to an Date (not to an column name), and parsing "X" to an Date is, lets say, a bit complicated - so the Exception is rised.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜