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

后端 未结 4 1250
失恋的感觉
失恋的感觉 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});
    

提交回复
热议问题