java.lang.IllegalArgumentException: Positional parameter does not exist: 1 in query解决

环境

Eclipse Indigo + Hibernate3.2 + Oracle 11gR2 + SQLPlus

 

问题

运行Hibernate程序,报如下异常

 

java.lang.IllegalArgumentException: Positional parameter does not exist: 1 in query: select id from contactor where name = ?
	at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:359)
	at org.hibernate.impl.AbstractQueryImpl.setInteger(AbstractQueryImpl.java:508)
	at com.addbook.dao.impl.ContactorDaoImpl.updateRecordByName(ContactorDaoImpl.java:75)
	at com.addbook.service.impl.ContactorServiceImpl.updateRecordByName(ContactorServiceImpl.java:50)
	at com.addbook.dao.impl.TestContactor.testUpdateRecordByName(TestContactor.java:67)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


 

解决

1.      一个语句里有多个赋值时采用引用占位符

2.       要更新的id是从以前的名字里查询得到,不建议通过名字查询id,再通过id update,而是直接在update语句里使用子查询。

 

 

//改成子查询
public int updateRecordByName(String originName, String newName){
		
		sql = "update contactor set name=:n where id=(select id from contactor where name=:na)";
				
		session = ConnUtil.getSession();
		session.beginTransaction();
		
		query = session.createSQLQuery(sql);
		query.setString("n", newName);
		query.setString("na", originName);
		int num = query.executeUpdate();
		session.getTransaction().commit();
		session.close();
		return num;
}


 

参考资料

http://txlaijava.iteye.com/blog/1032745

 

 

你可能感兴趣的:(Hibernate,ssh)