Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Spring Data JPA Parameter binding

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?

like image 757
Andrii Karaivanskyi Avatar asked Jan 01 '15 19:01

Andrii Karaivanskyi


People also ask

Which annotation binds a JPA query parameter to method argument?

Hence it is suggested to use @Param annotation in the method parameter to bind the query parameter names.

How do I use the same parameter multiple times in my query using JPA?

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.

What is @param annotation in spring boot?

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.

What is difference between JpaRepository and CrudRepository?

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.


2 Answers

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

like image 197
Xtreme Biker Avatar answered Oct 12 '22 20:10

Xtreme Biker


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.

like image 44
JB Nizet Avatar answered Oct 12 '22 22:10

JB Nizet