20

In my @Repository interface I created custom find method with JPQL @Query that contains parameter (addressType).

from Address a where a.addressType = :addressType

In the method I did not specify @Param("addressType") on the parameter. So I am getting

java.lang.IllegalArgumentException: Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions < 8.

Okay, this is pretty much clear, but I am using Java 8. So what is special about Java 8 here?

1
  • Oh dear, you have omitted "SELECT a" from your JPQL query, so it is now illegal. Commented Feb 17, 2016 at 8:07

3 Answers 3

18

The answer given by @JB Nizet is correct, but I just wanted to point out the way to add the -parameters flag for the Java 8 compiler when using Eclipse. This is in Window -> Preferences:

Preferences setting

Maven also allows adding the flags in the pom itself:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <compilerArgs>
            <arg>-verbose</arg>
            <arg>-parameters</arg>
        </compilerArgs>
    </configuration>
</plugin>

To add parameters flag for Java 8 compiler when using IDEA IntelliJ

File > Settings > Build, Execution, Deployment > Compiler > Java Compiler

Java Compiler setting

Sign up to request clarification or add additional context in comments.

4 Comments

Is this dependency or downlaod any jar files?
It's a maven plugin @Deva
So, this will increase my project size when I create war file?
@Aritz, Thank you sharing this and it worked like a charm... I added in the pom.xml as mentioned.
14

In Java 8, you can use reflection to access names of parameters of methods. This makes the @Param annotation unnecessary, since Spring can deduce the name of the JPQL parameter from the name of the method parameter.

But you need to use the -parameters flag with the compiler to have that information available.

See http://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html.

3 Comments

Thanks for the answer. But I'm using Java8 and SpringData still asks me for the @Param annotation.
Have you compiled your classes with the -parameters option, as explained in the page I linked to?
No I did not try that, I decided just to stick with @Param annotation since code will not work without special compilation configuration. But this your comment explains what you meant in your main answer. Thank you!
0

Answer from JB Nizet and Xtreme Biker are both corrects. I just want to add that if you use Spring Boot the -parameters compiler flag is already added for you by spring-boot-starter-parent (Gradle or Maven) :

plugin {
    delegate.groupId('org.apache.maven.plugins')
    delegate.artifactId('maven-compiler-plugin')
    configuration {
        delegate.parameters('true')
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.