Hibernate error:Named query not known

The error message

Named query not known: my.simple.query
    [junit] org.hibernate.MappingException: Named query not known: relationship.delete_by_userId
    [junit]  at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
    [junit]  at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1287)

The context

You are using a named query in your application. Named queries are externalized and are coded in the hibernate mapping files rather than the application code

Possible reason(s) for this error

1) The name of the query in the hibernate file doesnt match with what you have in the code. Both should match exactly

2) You have defined the query inside the class definition. A hibernate mapping file can have multiple class definitions (not a recomended or much practised approach though). A named query must exist outside the class definition.

Example(s)

<query name=”my.simple.query”>delete from Users as user where user.id =?</query

Session session = sessionFactory.openSession();
Query query = session.getNamedQuery(”my.simple.query”);
query.setInteger(0,userId);
session.beginTransaction();
query.executeUpdate ();
session.getTransaction().commit();

Did this solve your problem ?

Please let me know. Thanks !!!


Like it??? Save it !!!
  • Digg
  • del.icio.us
  • description
  • Reddit
  • TwitThis

3 comments to Hibernate error:Named query not known

  1. VENKAT
    November 26th, 2009 at 10:21 pm

    thanks for the solution.

  2. Dhaval Patel
    May 29th, 2010 at 1:58 pm

    I had same problem, I was getting Named query not known error, It has been solved by putting query definition out side class definition.

    Problem causing point was my query wan in side the class definition, it should be out side of class.

    I can able solved my problem after reading your solution.

    Thanks! lot

  3. Carlos David
    July 2nd, 2010 at 4:26 pm

    I have the same problem, but my hbm.xml files are generated by maven2-xdoclet2-plugin version 2.0.7, I don’t know what to do to fix it, I will shouldn’t edit hbm.xml files mannualy.

    If someone have a solution, please share it.

    Regards

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>