ssh根据姓名查询的时候报错java.lang.IndexOutOfBoundsException: Remember that

举报
穆雄雄 发表于 2022/12/17 11:17:51 2022/12/17
【摘要】 ​ 错误如下:java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:79)at o...

 错误如下:

java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!

at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:79)
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:85)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:421)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:393)
at org.dao.impl.EmpDaoImpl.queryInfoByName(EmpDaoImpl.java:118)
at org.service.impl.EmpServiceImpl.queryInfoByName(EmpServiceImpl.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy6.queryInfoByName(Unknown Source)
at org.service.impl.EmpServiceImplTest.testQueryInfoByName(EmpServiceImplTest.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
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)


相关代码如下:


/**(非 Javadoc)
	 * <p>Description(描述):根据员工的姓名查询员工信息 </p>
	 * <p>Title: queryInfoByName</p>
	 * @param name
	 * @return
	 * @see org.dao.IEmpDao#queryInfoByName(java.lang.String)
	 */

	@Override
	public Emp queryInfoByName(String name) {
		
		
		Emp emp = null;
		String hql = " from Emp where ename = ?";
		Session session = this.getSession();
		List<Emp> empList  =  session.createQuery(hql)
								.setParameter(1, name)
								.list();
		if(empList.size()!=0){
			emp = empList.get(0);
		}
		return emp;
	}






注意看这里:

setParameter(1, name)

很明显,这个地方的1应该改成0,但是我原来就是0来着,网上有人说要改成1.。。。。。。我以为我记错了?想着反正也是个错,何不试试呢。于是就二了起来试了试,也许之前越界是其他的原因,改到后面居然忘了到底改哪里了,于是乎把1重新改成了0之后终于可以用了。

setParameter(1, name)

正确详细代码如下:


/**(非 Javadoc)
	 * <p>Description(描述):根据员工的姓名查询员工信息 </p>
	 * <p>Title: queryInfoByName</p>
	 * @param name
	 * @return
	 * @see org.dao.IEmpDao#queryInfoByName(java.lang.String)
	 */

	@Override
	public Emp queryInfoByName(String name) {
		
		
		Emp emp = null;
		String hql = " from Emp where ename = ?";
		Session session = this.getSession();
		List<Emp> empList  =  session.createQuery(hql)
								.setParameter(0, name)
								.list();
		if(empList.size()!=0){
			emp = empList.get(0);
		}
		return emp;
	}

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。