Home > database >  Spring Data not working for Gradle projects under Eclipse without @Param annotations for method para
Spring Data not working for Gradle projects under Eclipse without @Param annotations for method para

Time:07-11

So I'm using Gradle, Eclipse, Java 11, Spring Boot with some Spring Data JPA repositories. In Eclipse, I've tested with 3 different JDK libraries configured in the build path and in the execution: OpenJDK, OracleJDK and GraalVM. I get the same error every time: Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all! Because of a line like:

@Query("select e from Entity e where e.value = :value")
Optional<Entity> findByValue(String value);

Guaranteed to work would be to add @Param("value") for the parameter, but I don't want to do that because this project has a lot of repositories. If I run this under the command line ./gradlew bootRun where I set one of the JDKs, this error does not occur.

If I try to reproduce this under a new project, but under Maven where I set the properties for java and maven for Java 11, then it works just fine under Eclipse.

The Gradle project works fine under a IntelliJ IDEA.

Can someone please help in explaining why this is happening? Does Eclipse bode well with Maven but not with Gradle? I can't fine an explanation.

CodePudding user response:

It's a compiler argument.

Seems that when the code is compiled, depending on the project, it will run with or without the -parameters argument to the javac command. When you run javac --help, the following line explains:

...
  -parameters
        Generate metadata for reflection on method parameters
...

This parameter is vital if you don't want to use @Param("value") in the repository method parameter, because this helps Spring Data determine the name of the parameter and assign it to :value.

In Eclipse(as of 2022-06), for your specific project, go to project Properties > Java Compiler and check "Store information about method parameters(usable via reflection)" and the project will work without "@Param" annotations in the repository method parameters: enter image description here

I still find it strange why on Maven projects this is activated but not on Gradle projects.

These answers helped me:

For IntelliJ IDEA, this might be a useful answer:

  • Related