1

I have a problem with the below JPA query in Apache Deltaspike:

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

It always results in the following exception:

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

However if I change it to positional parameters (which I would prefer not to do) it works:

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

Has anyone come across this before?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • Thanks for the suggestion, however this is a CDI project and doesn't use Spring. @QueryParam is the delta-spike equivalent. – StuPointerException Jul 24 '15 at 09:31
  • Does your interface have the `@Repository(forEntity=PersonPositionViewEntity)` annotation, and does it extend the EntityRepository? – Ben Green Jul 24 '15 at 09:49
  • 1
    Are you sure QueryParam can be used in this way? What's the fully-qualified name? I suspect you're using an annotation meant to map URI query parameters for web services. – Steve Chaloner Jul 24 '15 at 09:49
  • @SteveChaloner D'oh, I imported `javax.ws.rs.QueryParam` instead of `org.apache.deltaspike.data.api.QueryParam`. Thanks for your assistance! – StuPointerException Jul 24 '15 at 10:02

1 Answers1

0

You may notice that the parameter is picked up as an IndexedParameter instead of NamedParameter

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

What I usually do in such situations is step through the source code in debug mode and see why that happens. In your case, you need to pay attention to what happens in the following method of Parameters.java of DeltaSpike:

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]));
    }
}
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
Alin Pandichi
  • 955
  • 5
  • 15