Hibernate标准的日期 [英] Hibernate Criteria for Dates

查看:126
本文介绍了Hibernate标准的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在oracle中,我的日期格式为

2011年4月17日19:20:23.707000000

我想检索17-04-2011的所有订单。

  SimpleDateFormat formatter = new SimpleDateFormat(dd-MM-YYYY ); 
String myDate =17-04-2011;
日期日期= formatter.parse(myDate);
标准条件=
session.createCriteria(Order.class);
Criterion restrictDate = Restrictions.like(orderDate,date);

但它带给我空的结果:

解决方案

为什么使用 Restrictions.like(... )?



<您应该使用 Restrictions.eq(...)



请注意,您也可以使用 .le .lt .ge .gt 作为比较运算符的日期对象。 LIKE 运算符不适用于这种情况,因为 LIKE 当您希望根据部分内容柱。
请参阅 http://www.sql-tutorial.net/SQL-LIKE .asp 作为参考。



例如,如果您的名称列中包含某些人的全名,则可以执行,其中名称如'robert%',这样您将返回名称以'robert'可以替换任何字符)。



在你的情况下,你知道你想要匹配的日期的全部内容,所以你不应该使用 LIKE 但是相等。我猜Hibernate在这种情况下不会给你任何异常,但无论如何,你可能会遇到与 Restrictions.eq(...)相同的问题。



您使用代码得到的日期对象:

  SimpleDateFormat formatter = new SimpleDateFormat ( DD-MM-YYYY); 
String myDate =17-04-2011;
日期日期= formatter.parse(myDate);

这个日期对象相当于17-04-2011在0h,0分钟,0秒和0纳秒。

这意味着您在数据库中的条目必须恰好该日期。我的意思是,如果你的数据库条目有一个日期2011年4月17日19:20:23.707000000,那么它将不会被检索,因为你只是要求这个日期:2011年4月17日00:00: 00.0000000000。



如果您想要从给定日期检索数据库的所有条目,则必须使用以下代码:

  SimpleDateFormat formatter = new SimpleDateFormat(dd-MM-YYYY); 
String myDate =17-04-2011;
//创建日期17-04-2011 - 00h00
日期minDate = formatter.parse(myDate);
//创建日期18-04-2011 - 00h00
// - >我们取第一个日期,并以毫秒为单位添加1天,这要归功于一个有用的,并非众所周知的类
Date maxDate = new Date(minDate.getTime()+ TimeUnit.DAYS.toMillis(1));
连接和= Restrictions.conjunction();
//订单日期必须> = 17-04-2011 - 00h00
and.add(Restrictions.ge(orderDate,minDate));
//订单日期必须< 18-04-2011 - 00h00
and.add(Restrictions.lt(orderDate,maxDate));


In oracle I have dates in format

17-April-2011 19:20:23.707000000

I would like to retrieve all orders for 17-04-2011.

 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
Date date = formatter.parse(myDate);
    Criteria criteria = 
                session.createCriteria(Order.class);
Criterion restrictDate = Restrictions.like("orderDate",date); 

but it brings me empty result:

解决方案

Why do you use Restrictions.like(...)?

You should use Restrictions.eq(...).

Note you can also use .le, .lt, .ge, .gt on date objects as comparison operators. LIKE operator is not appropriate for this case since LIKE is useful when you want to match results according to partial content of a column. Please see http://www.sql-tutorial.net/SQL-LIKE.asp for the reference.

For example if you have a name column with some people's full name, you can do where name like 'robert %' so that you will return all entries with name starting with 'robert ' (% can replace any character).

In your case you know the full content of the date you're trying to match so you shouldn't use LIKE but equality. I guess Hibernate doesn't give you any exception in this case, but anyway you will probably have the same problem with the Restrictions.eq(...).

Your date object you got with the code:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
Date date = formatter.parse(myDate);

This date object is equals to the 17-04-2011 at 0h, 0 minutes, 0 seconds and 0 nanoseconds.

This means that your entries in database must have exactly that date. What i mean is that if your database entry has a date "17-April-2011 19:20:23.707000000", then it won't be retrieved because you just ask for that date: "17-April-2011 00:00:00.0000000000".

If you want to retrieve all entries of your database from a given day, you will have to use the following code:

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
    String myDate = "17-04-2011";
    // Create date 17-04-2011 - 00h00
    Date minDate = formatter.parse(myDate);
    // Create date 18-04-2011 - 00h00 
    // -> We take the 1st date and add it 1 day in millisecond thanks to a useful and not so known class
    Date maxDate = new Date(minDate.getTime() + TimeUnit.DAYS.toMillis(1));
    Conjunction and = Restrictions.conjunction();
    // The order date must be >= 17-04-2011 - 00h00
    and.add( Restrictions.ge("orderDate", minDate) );
    // And the order date must be < 18-04-2011 - 00h00
    and.add( Restrictions.lt("orderDate", maxDate) ); 

这篇关于Hibernate标准的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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