如何在休眠条件中转换日期类型 [英] How to conver date type in hibernate criteria

查看:99
本文介绍了如何在休眠条件中转换日期类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况。我在mysql数据库中有两种日期。一个是日期,另一个是日期时间。现在在休眠标准中,我必须检查一个日期是否大于另一个日期?

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,提交.date_received));

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

但是不同的类型导致显示 java.lang.ClassCastException的问题:java.lang.String无法转换为java.util.Date 。

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 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?

推荐答案

问题是 Restrictions.lt(字符串propertyName,对象值)是错误的限制。您需要的是 Restrictions.ltProperty(String propertyName,String otherPropertyName)

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

说明:


  • Restrictions.lt(String propertyName,Object value)是要比较的具有特定值的实体属性

  • Restrictions.ltProperty(String propertyName,String otherPropertyName)是比较两个实体属性

  • 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

如果使用 Restrictions.lt( Y, X)和 Y是日期属性的名称,然后休眠将尝试将 X转换为日期(而不是列名),并且将 X解析为日期可能有点复杂-因此

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.

这篇关于如何在休眠条件中转换日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆