我正在使用Hibernate 4.3.8.Final,Primefaces 6.0和MySQL Database 5.7.13。 我有一個表與這種結構的數據庫:SQL中的錯誤日期選擇
CREATE TABLE `rents` (
`rent_code` INT NOT NULL AUTO_INCREMENT,
`rent_daystart` datetime default NULL,
`rent_dayend` datetime default NULL,
PRIMARY KEY (`rent_code`)
) ENGINE = innodb CHARACTER SET utf8 COLLATE utf8_unicode_ci;
並與松鼠提取與下面的SQL以下數據:
select * from rents
rent_code | rent_daystart | rent_dayend
1 | 2016-11-30 16:03:00.0 | 2016-12-01 16:03:00.0
在我的Java bean,我有以下功能:
public List<Object> getRents(java.util.Date iniDate, java.util.Date endDate){
String SQL="select rent_code from rents where rent_daystart < :inidate and rent_dayend > :enddate";
List<Object> allRecords = null;
Session sesion=HibernateUtil.getSessionFactory().openSession();
try {
sesion.beginTransaction();
Query query = sesion.createSQLQuery(SQL).setDate("inidate", iniDate).setDate("enddate", endDate);
allRecords = query.list();
sesion.getTransaction().commit();
sesion.close();
}
catch (HibernateException he) {
//exception control code
};
return allRecords;
}
我執行網絡APP調試和功能接收的日期是:
**inidate** = 'Wed Nov 30 17:54:00 CET 2016'
**enddate** = 'Wed Nov 30 18:54:00 CET 2016'
並且它不返回任何記錄。
如果我松鼠那樣執行相同的SQL:
select rent_code from rents where rent_daystart < '2016-11-30 17:54:00' and rent_dayend > '2016-11-30 18:54:00'
它返回一個記錄。
我懷疑這是一個數據類型問題或類似的問題,但經過在網絡上的研究後,我不清楚。
有人可以幫我嗎?
在此先感謝!
如果您使用的是日誌框架,請嘗試啓用Hibernate [SQL級日誌記錄](http://stackoverflow.com/a/2536835/1255737)以確保其實際執行您認爲的查詢。 – rmlan