我正在使用java-hibernate-mysql组合
当我进行更新查询时,我遇到以下错误。我不明白hibernate有什么问题。 以下我发布了我得到的错误:
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)
有人可以告诉我为什么会收到此错误吗?
答案 0 :(得分:2)
您是否正在执行其中包含:
字符的sql字符串?如果是这样,Hibernate期待一个参数而你没有设置它。
String sql = "update SomeTable set someColumn = :value";
使用此功能,您通常会使用
设置值参数SQLQuery query = getSession().createSQLQuery(sql);
query.setString("value", "Some value with : in it");
或类似的。我只能假设你的值中有一个:
,它不代表参数,所以你应该把它建成一个字符串并将其设置为参数。
答案 1 :(得分: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});
答案 2 :(得分:0)
总结
1.你应该设置所有命名的参数 2.当您动态地将值设置为name param时,不应该:在其中。 如果你有:你应该使用字符串连接并删除命名param的方法。正如第一个答案所说。
答案 3 :(得分:0)
如果是动态的条件,你可以通过puttong绕过命名参数查询。
因此,您将其作为查询字符串,然后通过放置条件追加您的命名参数。
您不必放弃命名参数方法。