Issue
I have following code running on my local system:
@Query(value = "select l from TnncSetlmtSvcGrpStagingEntity l where l.runDate >=:runDate")
List<TnncSetlmtSvcGrpStagingEntity> findAllByRunDateGT(Date runDate);
But when I am running the same piece of code in higher environment I am getting the following error:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by:
<CDIMIServiceException>
<Message>Exception in CodeTableTp2Controller </Message>
<AdditionalInfo>null</AdditionalInfo>
<Cause>org.springframework.dao.InvalidDataAccessApiUsageException: For queries with named parameters you need to use provide names for method parameters.
Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.; nested exception is java.lang.IllegalStateException:
For queries with named parameters you need to use provide names for method parameters.
Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.</Cause> </CDIMIServiceException>
at com.visa.cdimi.code.table.tp2.CodeTableTp2Controller.execute(CodeTableTp2Controller.java:51)
at com.visa.cdimi.code.table.tp2.Application.main(Application.java:31)
... 8 more
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: For queries with named parameters you need to use provide names for method parameters.
Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.;
nested exception is java.lang.IllegalStateException: For queries with named parameters you need to use provide names for method parameters.
Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371)
I checked the java version on the server it is:
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b25, mixed mode)
On my local system also I am using Java 8.
Can anyone please help me solve this issue. Any help is appreciated.
Solution
Did you tried using a Named Parameter?
6.3.6. Using Named Parameters By default, Spring Data JPA uses position-based parameter binding, as described in all the preceding examples. This makes query methods a little error-prone when refactoring regarding the parameter position. To solve this issue, you can use @Param annotation to give a method parameter a concrete name and bind the name in the query
See https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.named-parameters for details.
So, this should work:
@Query(value = "select l from TnncSetlmtSvcGrpStagingEntity l where l.runDate >=:runDate")
List<TnncSetlmtSvcGrpStagingEntity> findAllByRunDateGT(@Param("runDate") Date runDate);
Answered By - Michael
Answer Checked By - David Marino (JavaFixing Volunteer)