HQL查询两个日期之间的记录 [英] HQL to query records between two dates
问题描述
我试图在我的Spring / Hibernate应用程序中使用HQL查询所有客户记录,这些记录在Date1和Date2之间有DateAdded,在Date1和Date2之间有LastSeen,所以我在Repository / DAO类中构建了这个HQL查询: p>
I am trying to query all customers records using HQL in my Spring/ Hibernate app that have DateAdded between Date1 and Date2 OR LastSeen between Date1 and Date2, so I've build this HQL query in the Repository/ DAO class:
sessionfactory.getCurrentSession().createQuery("from Customer c where c.dateAdded BETWEEN '"+startDate+"' AND '"+endDate+"' OR c.lastSeenDate BETWEEN '"+startDate+"' AND '"+endDate+"'").list();
我调试了应用程序以检查startDate和endDate,并发现它们是以
I've debugged the app to check the startDate and endDate and found that they are sent as:
startDate:Wed Jan 22 01:16:57 HKT 2014
startDate: Wed Jan 22 01:16:57 HKT 2014
endDate:Wed Jan 29 01:16: 57 HKT 2014
endDate: Wed Jan 29 01:16:57 HKT 2014
在数据库中,我100%确定至少有一条记录符合此查询,因为此记录的DateAdded和LastSeen如下所示:
In DB, I am 100% sure there is one record at least meeting this query, as this record DateAdded and LastSeen are as follows:
2014-01-23 15:33:38
2014-01-23 15:33:38
2014-01-25 15:33:38
2014-01-25 15:33:38
那么有人可以告诉我我在做什么错误/在这里丢失吗?
So can someone please tell me what I am doing wrong / missing here?
推荐答案
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String frmDate = format.parse(startDate);
String enDate = format.parse(endDate);
sessionfactory.getCurrentSession()
.createQuery("FROM Customer AS c WHERE c.dateAdded BETWEEN :stDate AND :edDate ")
.setParameter("stDate", frmDate)
.setParameter("edDate", enDate)
.list();
希望这会有所帮助!
这篇关于HQL查询两个日期之间的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!