19

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?

Jagger
  • 10,350
  • 9
  • 51
  • 93
Andrii Karaivanskyi
  • 1,942
  • 3
  • 19
  • 23

3 Answers3

16

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

crusy
  • 1,424
  • 2
  • 25
  • 54
Aritz
  • 30,971
  • 16
  • 136
  • 217
13

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.

DocZerø
  • 8,037
  • 11
  • 38
  • 66
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 2
    Thanks for the answer. But I'm using Java8 and SpringData still asks me for the @Param annotation. – Andrii Karaivanskyi Jan 01 '15 at 22:34
  • 3
    Have you compiled your classes with the -parameters option, as explained in the page I linked to? – JB Nizet Jan 02 '15 at 06:54
  • 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! – Andrii Karaivanskyi Jan 08 '15 at 14:18
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')
    }
}
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240