Java 8 Spring Data JPA Parameter binding

前端 未结 3 679
轻奢々
轻奢々 2021-01-11 09:35

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

from Address a where a.addressType = :addressT

相关标签:
3条回答
  • 2021-01-11 10:02

    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.

    0 讨论(0)
  • 2021-01-11 10:05

    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')
        }
    }
    
    0 讨论(0)
  • 2021-01-11 10:08

    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

    0 讨论(0)
提交回复
热议问题