简体   繁体   English

Hibernate Query在日期上提取记录而忽略时间戳

[英]Hibernate Query to fetch records on date ignoring timestamp

I have a timestamp column tradedate in one of the DB(Oracle) tables. 我在其中一个DB(Oracle)表中有一个时间戳列交换。 I am using hibernate as the persistence layer to fetch and store Data to DB. 我使用hibernate作为持久层来获取数据并将数据存储到数据库。 I have a requirement in which I need to query the DB on date. 我有一个要求,我需要在日期查询数据库。 ie From UI the user passes a date and I need to get the filtered data based on this date. 即,从UI用户传递日期,我需要根据此日期获取过滤后的数据。

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 例如:说2010年10月23日在DB 5中有10条记录,其中有时间戳条目,5条没有

So the query which i have returns me only the 5 rows which dont have timestamp. 所以我的查询只返回了没有时间戳的5行。

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 现在我知道我需要从时间戳中提取日期然后进行比较,以便我得到所有关于日期的记录,但我不知道它是如何完成Hibernate和使用HQL

the query in Java/Hibernate that i am using is as follows 我正在使用的Java / Hibernate中的查询如下

String hql = "select Clearing from POJO as POJO where POJO.tradeDate = :date"; String hql =“选择从POJO清除为POJO,其中POJO.tradeDate =:date”; Query query = getSession().createQuery(hql); 查询query = getSession()。createQuery(hql); query.setParameter("date", date); 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. 请注意,如果您按照上述查询,将不会使用tradeDate上的索引(如果有)。 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); 

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

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