2011-01-08 75 views
2

在HQL,我試圖用日期作爲標準得到一些數據,但我得到一個錯誤:HQL選擇的標準誤差

代碼:

Date DatePlaced=new Date(); 
     auction.setDatePlaced(DatePlaced); 
     auction.setUser((Users) getThreadLocalRequest().getSession(true).getAttribute("User")); 
     UpdateDatabase(auction); 
     Session session = gileadHibernateUtil.getSessionFactory().openSession(); 
     Long UserId=(Long) getThreadLocalRequest().getSession(true).getAttribute("UserId"); 
     String HQL="From Auction auction where User.UserId="+UserId +" and DatePlaced='"+DatePlaced+"'"; 
     Auction A1=(Auction) session.createQuery(HQL).list().get(0); 


Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.RangeCheck(ArrayList.java:547) 
    at java.util.ArrayList.get(ArrayList.java:322) 
    at com.BiddingSystem.server.ServiceImpl.UpdateAuction(ServiceImpl.java:543) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at net.sf.gilead.gwt.PersistentRemoteService.processCall(PersistentRemoteService.java:174) 

這裏發生的事情是主要是由於日期,但由於沒有數據被發現,因爲日期不匹配,但正在使用的數據進行搜索的同日我在上面設置成1行應該已返回

回答

3

嘗試使用命名參數,而不是試圖從字符串構建查詢

Auction A1=(Auction) session 
      .createQuery("From Auction auction where User.UserId=:userId and DatePlaced=:placed") 
      .setLong("userId",userId) 
      .setDate("placed",placed) 
      .list().get(0);