HQL Select using Date as criteria error
In HQL, I am trying to use date as a criteria get some data but i am getting an error:
Code:
Date DatePlaced=new Date();
auction开发者_Go百科.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)
What is happening here is mainly due to the date but because no data has been found because the date does not match but the data being used to search is the same date i have set above so 1 row should have been returned
try using named parameters instead of trying to build a query from a string
Auction A1=(Auction) session
.createQuery("From Auction auction where User.UserId=:userId and DatePlaced=:placed")
.setLong("userId",userId)
.setDate("placed",placed)
.list().get(0);
精彩评论