Thursday, November 3, 2011

queryparameterexception: could not locate named parameter

While testing my hibernate query I got a strange exception;
"nested exception is org.hibernate.QueryParameterException: could not locate named parameter".
I checked that the said parameter is in the hibernate model. Furthermore I checked there's a corresponding column in the database. So the problem was not about these. Later I saw that there's a problem while I was trying to set my named parameter. My hql query was like "from TableName t where t.field=:field1" and I was trying to set the value in "field1" with
query.setBoolean("field_", value);
As "field_" does not exist I got this error.
The correct form was;
query.setBoolean("field1", value);

5 comments:

  1. Hi
    I am using the below query in the same format as said by you.
    Query query = session.createQuery("select id.machineid from hibernate.ORMObjects.TerminalMachineMapping m where m.id.terminalid = :terminalid");
    query.setParameter("terminalid", "EJD001");

    Iterator ite = query.list().iterator();

    but getting the below exception

    org.hibernate.QueryParameterException: could not locate named parameter [terminalid]
    at org.hibernate.engine.query.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:99)
    at org.hibernate.engine.query.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:105)

    ReplyDelete
  2. Hi Kalpana,

    why you don't use query.setString("terminalid", "EJD001");
    This is probably due to this.

    ReplyDelete
  3. Hi Sezin,

    Thanks for your response.

    that is also throwing exception.

    java.lang.IllegalArgumentException: Parameter terminalid does not exist as a named parameter in [select m.id.machineid from hibernate.ORMObjects.TerminalMachineMapping m where m.id.terminalid = :terminalid]

    ReplyDelete
  4. Hi Kalpana,

    I can't exactly pinpoint your problem.
    I would check if there's a problem related to table names, column names etc, if the model mappings are right. For instance if you're trying to query terminalid but you dont have it in your model it would generate an exception. Another case is the case where your terminalid is long or integer but youre trying to set with setString. Another case is where there's a problem with your hql query which seems ok to me in your case.

    ReplyDelete
  5. Query q1=ses.createQuery("select st from StudentDemo as st where st.smail like ? and st.sname in(:p2,:p3) order by st.sid desc");
    // set parameter
    q1.setInteger("@gmail.com", 0);
    q1.setString("p2", "subbu");
    q1.setString("p3", "raju");
    //execute hql query
    List l=q1.list();

    this when i was executed that time
    Parameter @gmail.com does not exist as a named parameter in [select st from StudentDemo as st where st.smail like ? and st.sname in(:p2,:p3) order by st.sid desc].
    this error was came how to reslove any one please help me.

    ReplyDelete