1 Reply Latest reply on Jan 15, 2006 8:53 AM by knifegun

    Problem with EJB QL query

    Fabricio Braga Newbie

      Hi all,

      I'm getting in trouble while executing a simple query. I have an Entity named "Project", and a Session named ProjectServiceBean that implements ProjectService interface.

      So the ProjectService (remote interface) gives me a method like this:

      public Project findByName(String name) throws RemoteException {
       return (Project)entityManager.createQuery("from Project p where p.name = name").getSingleResult();
       }
      


      Into my table "PROJECT" I don't have any duplicate names for Projects, but when accessing this method from a client, I got a message error at JBoss console that means there is a problem because that query returns more than one register!

      So, I changed the method signature and tested the following:


      public Collection <Project> findByName(String name) throws RemoteException {
      Collection <Project> result = entityManager.createQuery("from Project p where p.name = name").getResultList();
      return result;
      }


      And then I got a Collection with all registers from table "PROJECT" !

      I seems that problem is something about my EJB QL sintax. So I made another change...

      public Project findByName(String name) throws RemoteException {
       return (Project)entityManager.createQuery("from Project p where p.name = '" + name + "'").getSingleResult();
       }
      


      And it works! Finally I got only one register from my query. But it does not seems to be so ellegant syntax, and it's different from syntax used at JBoss 3.0 TrailBlazer.

      So I ask you:
      Why my first code returns all registers from table?
      What is the best approach to find objects using EJB QL?
      Is that possible to pass parameters using "?1", "?2", "?n" syntax? How?

      Thank you all, for your attemption.
      Fabricio Braga