Java 8 Spring Data JPA Parameter binding

故事扮演 提交于 2019-12-01 13:55: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:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/27733892/java-8-spring-data-jpa-parameter-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!