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?
Hence it is suggested to use @Param annotation in the method parameter to bind the query parameter names.
If for some reason we do need to use the same parameter many times within the same query, we just need to set it once by issuing the “setParameter” method. At runtime, the specified values will replace each occurrence of the parameter.
In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.
CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.
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:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With