简体   繁体   English

休眠条件查询不适用于日期

[英]Hibernate Criteria Query not working with date

The following line of code works perfectly when tested with string 当使用字符串测试时,以下代码行可以完美地工作

criteria.add(Restrictions.eq("name", "John"));

However when i test it with Date it returns error This is line of code that returns error 但是,当我用Date测试它返回错误时,这是​​返回错误的代码行

criteria.add(Restrictions.eq(currentDate,dd));

This is how i am getting currentDate 这就是我得到currentDate的方式

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String currentDate=dateFormat.format(cal.getTime());

I printed currentDate and dd and following is output in console along with the error 我打印了currentDatedd ,以下内容与错误一起在控制台中输出

Current Date:2014-02-17 dd:2014-02-16 当前日期:2014-02-17 dd:2014-02-16

JKInsrException:->could not resolve property: 2014-02-17 of: com.java.JKInsr.Contact JKInsrException:->无法解析属性:2014年2月17日的com.java.JKInsr.Contact

Restrictions.eq() takes a property-name as first argument, not a value or object. Restrictions.eq()将属性名称作为第一个参数,而不是值或对象。

You should do it like this: 您应该这样做:

Restrictions.eq("myDate", dd)

Make sure your Contact class has the myDate-Attribute. 确保您的Contact类具有myDate-Attribute。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM