2014-01-21 118 views
10

我想在我的Spring/Hibernate應用程序中使用HQL查詢所有客戶記錄,這些記錄在Date1和Date2之間或Date1和Date2之間有LastSeen,所以我在這個HQL查詢中創建了這個HQL查詢存儲庫/ DAO類:HQL查詢兩個日期之間的記錄

sessionfactory.getCurrentSession().createQuery("from Customer c where c.dateAdded BETWEEN '"+startDate+"' AND '"+endDate+"' OR c.lastSeenDate BETWEEN '"+startDate+"' AND '"+endDate+"'").list(); 

我調試的應用程序來檢查起始日期和結束日期,發現它們發送爲:

開始日期:星期三1月22日1時16分57秒香港時間2014

結束日期:Wed Jan 29 01:16:57 HKT 2014

在DB,我100%肯定有一個記錄至少會這個查詢,因爲這是記錄和DateAdded LastSeen如下:

2014年1月23日15時33分38秒

2014 -01-25 15:33:38

那麼有人可以告訴我我在做什麼錯誤/在這裏失蹤?

+2

我不確定在日期作爲文件傳遞時如何在HQL中處理時區,但是您應該*真正*使用查詢參數而不是字符串連接。 – Guillaume

+0

@Guillaume這是專門解決這個問題還是一般來說? – MChan

+0

@Guillaume將其更改爲sessionfactory.getCurrentSession()。createQuery(「from Customer c where c.dateAdded BETWEEN:startD AND:endD」).setParameter(「startD」,startDate).setParameter(「endD」,startDate).list ();並仍然不會工作:( – MChan

回答

-1

嘗試這樣:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Date fromDate = df.parse(startDate); 
Date toDate = df.parse(endDate); 
Criteria criteria = sessionfactory.getCurrentSession().createCriteria(Customer.class) 
    .add(Restrictions.between("dateAdded", fromDate , toDate)); 
List<Customer> listCustomer = criteria.list(); 

希望這有助於.. !!

+1

這將無法正常工作,因爲您將格式化日期返回到Date對象的所有格式都不見了 – MChan

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

希望這會有所幫助!

+0

這不會工作,因爲你返回格式化日期回到日期對象所有格式不見了 – MChan

+0

@MChan檢查我的更新 –

+0

這也不會工作:(因爲setParameter將檢查傳遞的變量類型。因此它會引發在String和Date之間失敗的例外,即使你試圖在setParameter中投射,它也不會起作用 – MChan

5

這是一箇舊帖子,但我想它可能有助於某人。設置.setTimeStamp應該做的伎倆。

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 ") 
.setTimestamp("stDate", frmDate) 
.setTimestamp("edDate", enDate) 
.list(); 
2
SimpleDateFormat sf=new SimpleDateFormat("dd-MM-YYYY"); 
String fromDate=null; 
String toDate=null; 
fromDate=sf.format(startDate); 
toDate=sf.format(endDate); 
sessionfactory.getCurrentSession().createQuery("from Customer where dateAdded BETWEEN '"+startDate+"' AND '"+endDate+"'"); 
+1

你應該解釋你的答案,而不僅僅是郵政編碼。 – Martin

0

我有類似的問題。當我們使用結束日期(1月29日星期三)時,系統會尋找1月29日星期三00:00:00,就像選擇1月28日一樣。爲了避免這種情況,我們可以在結束日期添加一天。這也解釋了爲什麼我們沒有開始日期的問題。

令人驚訝的是,當我們使用hibernate標準時,這個問題並不存在。

0
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 
    Calendar c = Calendar.getInstance(); 
    c.setTime(new Date()); // Now use today date. 
    c.add(Calendar.DATE, 90); 
    Date fromDate = null, toDate = null; 
    String fromDateStr = formatter.format(new Date()); 
    String toDateStr = formatter.format(c.getTime()); 
    try { 
     fromDate = formatter.parse(fromDateStr); 
     toDate = formatter.parse(toDateStr); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    query.setParameter("stDate", fromDate); 
    query.setParameter("edDate", toDate); 
相關問題