日期:2014-05-20  浏览次数:20965 次

Hibernate的占位符问题
下面的这段代码在JPA环境是可以运行通过的,但是在hibernate下面却不行,就是这句
update Catalog c set c.visible=?1 where c.id in ("+ buffer.toString()+ ")";不能通过;

后来我改了一个简单一点的"update News n set n.visible=? where n.id=?")
.setParameter(0, false).setParameter(1,newsid).executeUpdate();
这句在Hibernate下面可以运行通过,貌似Hibernate不支持“?1,?2”这种形式的 那我要怎么组拼SQL语句完成where c.id in ("+ buffer.toString()+ ")");这句的功能呢?


public <T> void deletes(Class<T> entityClass, Object[] entityIds) {
if(entityIds!=null && entityIds.length>0){
StringBuffer buffer = new StringBuffer("");
for(int i=0 ;i<entityIds.length; i++){
buffer.append("?").append(i+2).append(",");
}
buffer.deleteCharAt(buffer.length()-1);
Query query = sessionFactory.getCurrentSession().createQuery("update Catalog c set c.visible=?1 where c.id in ("+ buffer.toString()+ ")");
query.setParameter(1,false);
for(int i=0 ;i<entityIds.length; i++)
query.setParameter(2+i, entityIds[i]);
query.executeUpdate();
query = sessionFactory.getCurrentSession().createQuery("update News o set o.visible=?1 where o.catalog.id in ("+ buffer.toString()+ ")");
query.setParameter(1, false);
for(int i=0 ;i<entityIds.length; i++)
query.setParameter(2+i, entityIds[i]);
query.executeUpdate();
}
}
上面代码的错误提示:java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:397)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:369)
at jiao.zhi.chao.dao.impl.NewsDaoImpl.delete(NewsDaoImpl.java:26)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy11.delete(Unknown Source)
at jiao.zhi.chao.junit.NewsDaoImplTest.testdelete(NewsDaoImplTest.java:32)
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.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)