Results 1 to 9 of 9

Thread: Named Query not known

  1. #1
    Join Date
    May 2006
    Posts
    6

    Default Named Query not known

    Hello,

    In my hbm.xml file, I have the following query

    <query name="user.find_byUserId"><![CDATA[from com.elemica.api.component.user.UserBean as u where u.userId=:userId]]></query>

    In my java class, I'm calling the above query using

    String[] paramNames = {"userId"};
    Object[] paramValues = {userId};
    return getHibernateTemplate().findByNamedQueryAndNamedPar am("find_byUserId", paramNames, paramValues);

    When I execute the class, I get the following error

    org.springframework.orm.hibernate3.HibernateSystem Exception: Named query not known: find_byUserId; nested exception is org.hibernate.MappingException: Named query not known: find_byUserId

    Could anyone pls let me know where the mistake could be.

    Thanks in advance.
    Sam

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    If I see that right, you name the query "user.find_byUserId" but trying to use "find_byUserId".

    Regards,
    Andreas

  3. #3
    Join Date
    May 2006
    Posts
    6

    Default

    Quote Originally Posted by Andreas Senft
    If I see that right, you name the query "user.find_byUserId" but trying to use "find_byUserId".

    Regards,
    Andreas
    I have corrected my class to use the correct query name.

    String[] paramNames = {"userId"};
    Object[] paramValues = {userId};
    return getHibernateTemplate().findByNamedQueryAndNamedPar am("user.find_byUserId", paramNames, paramValues);

    But I still get the same error

    org.springframework.orm.hibernate3.HibernateSystem Exception: Named query not known: find_byUserId; nested exception is org.hibernate.MappingException: Named query not known: user.find_byUserId

    I have also checked to see if there are any multiple hbm.xml with the same name, but there is only one that I'm using. Is there anything else that I can check for.

    Thanks
    Sam

  4. #4
    Join Date
    Jul 2005
    Location
    Lübeck, Germany
    Posts
    9

    Default

    Hey spagilla,

    it has been a while since you posted this thread and i think you actually solved this problem. I had it too and found your thread but no solution. After some time i found out whats wrong and want't to post my solution for all the others having this problem.

    I made the mistake to place the <query> element inside the <class> one. Hibernate in combination with Spring didn't find it there. To fix it just place the <query> element outside the <class> one but inside the <hibernate-mapping> element.

    Greetz AngryAngel

  5. #5

    Default

    Quote Originally Posted by angryangel9 View Post
    Hey spagilla,

    it has been a while since you posted this thread and i think you actually solved this problem. I had it too and found your thread but no solution. After some time i found out whats wrong and want't to post my solution for all the others having this problem.

    I made the mistake to place the <query> element inside the <class> one. Hibernate in combination with Spring didn't find it there. To fix it just place the <query> element outside the <class> one but inside the <hibernate-mapping> element.

    Greetz AngryAngel

    Thanks AngryAngel for posting the solution

  6. #6

    Default

    Thanks, that solved my problem!
    Last edited by WinterSolutions; Nov 29th, 2007 at 03:08 PM.

  7. #7

    Default

    Thanks Angryangel. I too have done exactly the same mistake and it solved my problem.

  8. #8
    Join Date
    Feb 2008
    Posts
    1

    Default Another Cause of this error

    Check the name of your mapping files! I'd mistakenly named one of my mapping files (.xml) instead of (.hbm.xml), and didn't spot it as they all end with (...xml).

    NH didn't even compile in my mapping file, so I got no errors, or indications. GetNamedQuery() didn't have a clue what I was on about because the mapping file didn't even exist as far as NH was concerned.

    I spent hours, checking and re-checking my mapping file, retyping code 'just incase' there was a typo in there i couldn't see etc.

    If you get this error, check the extensions on your mapping files FIRST, before going looking for a more complicated cause.

  9. #9
    Join Date
    Jan 2012
    Posts
    1

    Default

    Thank you AngryAngel, you are an angel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •