日期的休眠条件 [英] Hibernate Criteria for Dates

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

问题描述

在oracle中我有日期格式

In oracle I have dates in format

2011 年 4 月 17 日 19:20:23.707000000

17-April-2011 19:20:23.707000000

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

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:

推荐答案

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

你应该使用Restrictions.eq(...).

注意你也可以在日期对象上使用.le.lt.ge.gt作为比较运算符.LIKE 运算符不适用于这种情况,因为 LIKE 在您想根据列的部分内容匹配结果时很有用.请参阅http://www.sql-tutorial.net/SQL-LIKE.asp 供参考.

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.

例如,如果您有一个包含某些人全名的姓名列,您可以执行 where name like 'robert %' 以便您将返回名称以 'robert 开头的所有条目' (% 可以替换任何字符).

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).

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

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);

这个日期对象等于 17-04-2011 在 0h、0 分、0 秒和 0 纳秒.

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

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

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) ); 

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

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