1

我正在使用 java-hibernate-mysql 组合

当我进行更新查询时,我得到以下错误。我不明白休眠有什么问题。在我发布了我得到的错误之后:

org.hibernate.QueryException: Not all named parameters have been set: [0] [update       sequence s set s.cmd_output='neox     tty1         2012-06-08 09:40 (:0)
neox     pts/1        2012-06-08 09:41 (:0)
neox     pts/0        2012-06-08 09:41 (:0)
neox     pts/2        2012-06-08 09:41 (:0)
neox     pts/3        2012-06-08 12:48 (deval-PC.local.lan)
[neox@localhost ~]$ ', s.cmd_output_time='2012-06-08 12:48:58' where s.id=43]
at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:106)
at org.hibernate.impl.QueryImpl.executeUpate(QueryImpl.java:85)
at db_model.sequence_db.insert_Sequence_new(sequence_db.java:242)
at views.CaptureNavigationView$10.widgetSelected(CaptureNavigationView.java:555)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at nspl.test.ui.Application.start(Application.java:43)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at    org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLaunch    er.java:110)
at     org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

谁能告诉我为什么我得到这个错误?

4

4 回答 4

3

您是否正在执行带有:字符的 sql 字符串?如果是这样,Hibernate 需要一个参数,而您没有设置它。

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

使用它,您通常会使用设置 value 参数

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

或类似的。我只能假设您的值中有一个:不表示参数的值,因此您应该将其构建为字符串并将其设置为参数。

于 2012-06-08T07:35:41.337 回答
1

问题在于这里的粗体部分:2012-06-08 09:41 ( :0 )

您不应该连接 String 来构建您的查询,而是使用参数。这是在 hql 查询中转义 : 字符的唯一方法。

例子 :

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});
于 2012-06-08T07:42:44.487 回答
0

总结

1.你应该设置所有命名参数 2.当你动态设置值到命名参数时,不应该有 : 在里面。如果你有:你应该使用字符串连接并放弃命名参数的方法。如第一个答案所述。

于 2012-06-08T07:48:45.997 回答
0

如果条件是动态的,您可以通过 puttong 绕过命名参数查询。

因此,您将其作为查询字符串,然后通过放置条件来附加您的命名参数。

您不必删除命名参数方法。

于 2012-10-11T06:40:25.623 回答