2011-01-08 76 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);