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 ValueRestrictions.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.
精彩评论