How to solve “org.hibernate.QueryException: Not all named parameters have been set” error?

后端 未结 4 1219
失恋的感觉
失恋的感觉 2021-01-24 20:38

I am using java-hibernate-mysql combination

When i m doing update query i m getting following error. I don\'t understand what is wrong with hibernate. Following i have

相关标签:
4条回答
  • 2021-01-24 21:20

    The issue is with the part in bold here : 2012-06-08 09:41 (:0)

    You should not concatenate String to build your query but instead use parameters. It is the only way to escape the : characters in hql queries.

    Example :

    String param1 = "neox     tty1         2012-06-08 09:40 (:0)\n" +
                    "neox     pts/1        2012-06-08 09:41 (:0)\n"+
                    "neox     pts/0        2012-06-08 09:41 (:0)\n" +
                    "neox     pts/2        2012-06-08 09:41 (:0)\n" +
                    "neox     pts/3        2012-06-08 12:48 (deval-PC.local.lan)\n" +
                    "[neox@localhost ~]$ ";
    String param2 = "2012-06-08 12:48:58";
    String id = 43;
    
    String hqlQuery = "update sequence s set s.cmd_output = :cmd_output, " +
                      "s.cmd_output_time = :cmd_output_time where s.id = :cmdId";
    
    getHibernateTemplate().findByNamedParam(hqlQuery, 
       new String[] {"cmd_output", "cmd_output_time", "cmdId"},
       new Object[] {param1, param2, id});
    
    0 讨论(0)
  • 2021-01-24 21:22

    to summarise

    1.you should set all named params 2. when you dynamically set values to name param, there should not be : in it. in case you have : you should use string concatenation and drop the approach of named param. as said in first answer.

    0 讨论(0)
  • 2021-01-24 21:31

    Are you executing an sql string with the : character in them? If so, Hibernate is expecting a parameter and you're not setting it.

    String sql = "update SomeTable set someColumn = :value";
    

    Using this you would usually set the value parameter using

    SQLQuery query = getSession().createSQLQuery(sql);
    query.setString("value", "Some value with : in it");
    

    or similar. I can only assume your value has a : in it which does not signify a parameter so you should build this as a string and set that as the parameter.

    0 讨论(0)
  • 2021-01-24 21:33

    You can bypass the named parameter query by puttong if condition when it is dynamic.

    So you make it as a query string and then append your named parameter by putting a condition.

    You don't have to drop the named parameter approach.

    0 讨论(0)
提交回复
热议问题