HQL Select usando la fecha como error de criterio

In HQL, I am trying to use date as a criteria get some data but i am getting an error:


Date DatePlaced=new Date();
        auction.setUser((Users) getThreadLocalRequest().getSession(true).getAttribute("User"));
        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

preguntado el 08 de enero de 11 a las 22:01

1 Respuestas

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")

Respondido el 09 de enero de 11 a las 01:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.