JPA查询参数不起作用

时间:2015-07-24 09:08:19

标签: java hibernate jpa deltaspike

我在Apache Deltaspike中遇到以下JPA查询的问题:

@Query("from PersonPositionViewEntity where forename1 like :forename1 and surname like :surname")
List<PersonPositionView> search(@QueryParam("forename1") String forename1, @QueryParam("surname") String surname);

它总是会导致以下异常:

Caused by: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
    at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:518) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:674) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:198) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at org.apache.deltaspike.data.impl.param.IndexedParameter.apply(IndexedParameter.java:40) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    at org.apache.deltaspike.data.impl.param.Parameters.applyTo(Parameters.java:120) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    at org.apache.deltaspike.data.impl.builder.AnnotatedQueryBuilder.createJpaQuery(AnnotatedQueryBuilder.java:80) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    at org.apache.deltaspike.data.impl.builder.AnnotatedQueryBuilder.execute(AnnotatedQueryBuilder.java:48) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    at org.apache.deltaspike.data.impl.builder.QueryBuilder.executeQuery(QueryBuilder.java:57) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    at org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeNonTransactional(TransactionalQueryRunner.java:66) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    at org.apache.deltaspike.data.impl.tx.TransactionalQueryRunner.executeQuery(TransactionalQueryRunner.java:61) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    at org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:79) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]
    ... 84 common frames omitted

但是,如果我将其更改为位置参数(我不想这样做),它可以工作:

@Query("from PersonPositionViewEntity where forename1 like ?1 and surname like ?2")
List<PersonPositionView> search(String forename1, String surname);

以前有人遇到过这个吗?

1 个答案:

答案 0 :(得分:0)

您可能会注意到该参数被选为IndexedParameter而不是NamedParameter

at org.apache.deltaspike.data.impl.param.IndexedParameter.apply(IndexedParameter.java:40) ~[deltaspike-data-module-impl-1.3.0.jar:1.3.0]

在这种情况下我通常做的是在调试模式中逐步完成源代码,看看为什么会发生这种情况。在您的情况下,您需要注意DeltaSpike的Parameters.java的以下方法中发生的事情:

public static Parameters create(Method method, Object[] parameters) {
    ...
    QueryParam qpAnnotation = extractFrom(annotations[i], QueryParam.class);
    if (qpAnnotation != null) {
        result.add(new NamedParameter(qpAnnotation.value(), parameters[i]));
    }
    else {
        result.add(new IndexedParameter(paramIndex++, parameters[i]));
    }
}