Hibernate Query在日期取回记录,忽略时间戳 [英] Hibernate Query to fetch records on date ignoring timestamp

查看:112
本文介绍了Hibernate Query在日期取回记录,忽略时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间戳列在一个DB(Oracle)表中交易。
我使用hibernate作为持久层来获取数据并将其存储到数据库。
我有一个需求,我需要在日期查询数据库。
即用户界面用户通过日期,我需要根据此日期获取过滤的数据。



如果交易所列仅包含日期部分,查询返回正确的记录
当交易列填充时间戳值即(日期+时间)时出现问题。然后这些值不会被查询返回。



例如:假设2010年10月23日有10条记录,其中有5条记录有时间戳条目, p>

所以,我有的查询只返回5行,没有时间戳。



现在我知道我需要从时间戳提取日期,然后做比较,以便我得到所有记录的特定日期,但我不知道它如何完成与Hibernate和使用HQL



我在使用的 Java / Hibernate 中的查询如下所示:

String hql =select从POJO清除POJO POJO.tradeDate =日期;
Query query = getSession()。createQuery(hql);
query.setParameter(date,date);

解决方案

  String hql =from POJO as POJO where to_date(to_char(POJO.tradeDate ,'DD-MON-YY'),'DD-MON-YY')=:date; 
Query query = getSession()。createQuery(hql);
query.setParameter(date,date);



请注意,如果您遵循上述查询,则不会使用 tradeDate 上的任何索引。如果有一些性能问题,你可以这样做,

  String hql =从POJO作为POJO其中POJO.tradeDate:date和:ceilDate之间的日期; 
Query query = getSession()。createQuery(hql);
//具有时间戳部分的日期,00:00:00.0或完全缺失
query.setParameter(date,date);
//具有时间戳部分的日期,23:59:59.999
query.setParameter(ceilDate,ceilDate);


I have a timestamp column tradedate in one of the DB(Oracle) tables. I am using hibernate as the persistence layer to fetch and store Data to DB. I have a requirement in which I need to query the DB on date. i.e From UI the user passes a date and I need to get the filtered data based on this date.

If the tradedate column only has the date part my query returns the correct records The issue arises when the tradedate column is populated with a timestamp value ie(date + time). Then those values are not returned by the query.

eg:say there are 10 records for 23rd Oct 2010 in DB 5 of them have the timestamp entries and 5 dont

So the query which i have returns me only the 5 rows which dont have timestamp.

Now i know that i need to extract date from the timestamp and then do the comparision so that i get all the records for aprticular date but i dont know how its done with Hibernate and using HQL

the query in Java/Hibernate that i am using is as follows

String hql = "select Clearing from POJO as POJO where POJO.tradeDate = :date"; Query query = getSession().createQuery(hql); query.setParameter("date", date);

解决方案

String hql = "from POJO as POJO where to_date(to_char(POJO.tradeDate, 'DD-MON-YY'), 'DD-MON-YY') = :date"; 
Query query = getSession().createQuery(hql); 
query.setParameter("date", date); 

[Edit]

Beware, the index, if there is any, on tradeDate will not be used if you follow the above query. In case, there are some performance concerns, you can rather do it like this,

String hql = "from POJO as POJO where POJO.tradeDate between :date and :ceilDate"; 
Query query = getSession().createQuery(hql); 
// a date having timestamp part, 00:00:00.0, or missing completely
query.setParameter("date", date); 
// a date having timestamp part, 23:59:59.999
query.setParameter("ceilDate", ceilDate); 

这篇关于Hibernate Query在日期取回记录,忽略时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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