我在通过 JPA 查询数据库时遇到问题。有异常:
org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:80)
at org.hibernate.engine.query.spi.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:86)
at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:444)
at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:416)
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:440)
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:67)
at com.lawa.service.impl.SubjectServiceImpl.getTotal(SubjectServiceImpl.java:746)
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:601)
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.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
我的代码是:
@Transactional(propagation=Propagation.REQUIRED)
public long getTotal(PSubject search) {
StringBuilder jsql = new StringBuilder("select count(*) from PSubject p where p.id=p.id ");
ArrayList paramValues = new ArrayList();
if(search.getName()!=null && search.getName().trim().length()>0) {
jsql.append(" and p.name=:name");
paramValues.add(search.getName().trim());
}
if(search.getType()!=null && search.getType().trim().length()>0) {
jsql.append(" and p.type=:type");
paramValues.add(search.getType().trim());
}
if(search.getMacaddress()!=null && search.getMacaddress().trim().length()>0) {
jsql.append(" and p.macaddress=:macaddress");
paramValues.add(search.getMacaddress().trim());
}
if(search.getUri()!=null && search.getUri().trim().length()>0) {
jsql.append(" and p.uri=:uri");
paramValues.add(search.getUri().trim());
}
if(search.getDescription()!=null && search.getDescription().trim().length()>0) {
jsql.append(" and p.description=:description");
paramValues.add(search.getDescription().trim());
}
if(search.getCreateTime()!=null) {
jsql.append(" and p.createTime=:createTime");
}
Query query = persistService.getEntityManager().createQuery(jsql.toString());
for(int i=0;i<paramValues.size();i++) {
query.setParameter((i+1), paramValues.get(i));
}
if(search.getCreateTime()!=null) {
query.setParameter(":createTime",search.getCreateTime(),TemporalType.DATE);
}
Long count = (Long)query.getSingleResult();
return count;
}
首先我想让代码工作,我不喜欢代码。我觉得不干净,我要一个一个地验证“PSubject”的每个属性并联系jpql,我必须选择“createTime”,因为它的类型是“Date”。更何况我在jpql里面加了“p.id=p.id”,感觉不太好。
请帮助我解决异常并提供最佳实践。