Is it possible to get rid of @Param Annotation in Spring Data JPA Repository

Viewed 694

In Spring Data JPA, when I want to write a custom query, if I specify a parameter, I send this parameter value with @Param annotation, Is it possible to get rid of @Param Annotation in Spring Data JPA Repository?

Example Query:

@Query("select u from User where u.name = :name")
User findUserByName(@Param("name") String name);

Desired Query:

@Query("select u from User where u.name = :name")
User findUserByName(String name);

Note, I don't want to use ?0 or something like that I want to use directly name parameters.

1 Answers

Yes, this is perfectly possible. As the Reference describes:

As of version 4, Spring fully supports Java 8’s parameter name discovery based on the -parameters compiler flag. By using this flag in your build as an alternative to debug information, you can omit the @Param annotation for named parameters.

And Stackoverflow has an example how to do that with Maven (and probably for the build tool of your choice as well):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <compilerArgs>
                    <arg>-parameters</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
Related