2 Replies Latest reply on Jun 4, 2008 4:51 PM by Johannes Koch

    JPQL issue - org.hibernate.QueryParameterException: could no

    Johannes Koch Newbie

      I am trying to execute two JPQL queries in an EJB3 application deployed on a JBoss 4.2.2.GA. The code looks as follows:

      String queryString1 = "select sub from Subscription sub where sub.itemPattern in (select ip from ItemPattern ip where ip.caption = :c)";
      List<?> queryResultList = em.createQuery(queryString1).setParameter("c", "myCaption").getResultList();
      String queryString2 = "select itm from green20.demo.items.Item itm where itm.persistenceID =:p";
      List<?> queryResultList2 = em.createQuery(queryString2).setParameter("p", 2).getResultList();
      

      While the first query works fine, the second one results in the following error:
       org.hibernate.QueryParameterException: could not locate named parameter [p]
      

      Even more strange: When I try to switch to a numbered parameter, like this
      String queryString2 = "select itm from green20.demo.items.Item itm where itm.persistenceID =?1";
      List<?> queryResultList2 = em.createQuery(queryString2).setParameter(1, 2).getResultList();
      

      the error message stays the same and still complains about a named parameter:
       org.hibernate.QueryParameterException: could not locate named parameter [1]
      

      This is also not affected by whether I pass the parameter value as int (2), String ("2") or Integer (new Integer(2)).
      The only difference I can notice between the two queries is that in the second one I'm using a fully qualified name for the persisted Entity because the class is defined in a different package. Maybe Hibernate gets confused by that?

      Can anybody give me a hint? I'm totally out of ideas.



        • 1. Re: JPQL issue - org.hibernate.QueryParameterException: coul
          jaikiran pai Master

           

          "JoPe" wrote:

          The only difference I can notice between the two queries is that in the second one I'm using a fully qualified name for the persisted Entity because the class is defined in a different package. Maybe Hibernate gets confused by that?

          Can anybody give me a hint? I'm totally out of ideas.



          From what i remember, this is a case of Hibernate reporting the wrong error message. I guess, the actual error is "mapping not found for green20.demo.items.Item". Try this query:

          String queryString2 = "select itm from green20.demo.items.Item itm";


          This probably will show you the correct error message. And once you fix that, you can change it to accept the parameters.



          • 2. Re: JPQL issue - org.hibernate.QueryParameterException: coul
            Johannes Koch Newbie

            Thank you, jaikiran!

            You were almost right: The actual error was that the Item class wasn't mapped, which is what made me use the fully qualified name in the first place.

            However, note:

            String queryString2 = "select itm from green20.demo.items.Item itm";
            

            does NOT throw any exception or error message AT ALL (also not in server log). Once I replaced the fully qualified name, it showed the correct exception again.