Ishan Tiwary
Ishan Tiwary

Reputation: 1008

Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters

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.

Upvotes: 16

Views: 21933

Answers (2)

AguThadeus
AguThadeus

Reputation: 330

This issue occurs when method parameter names are not available to be read by reflection, the javac -parameters flag ensures that these parameters are available during runtime. If you somehow encounter this issue, make sure your classes are compiled with -parameters, in Eclipse you can do this by going to Window > Preferences > Java > Compiler then make sure the option "Store information about method parameters" is checked.

Alternatively if you use per project settings go to your project "Right Click" > Properties (Shortcut: Alt+Enter), navigate to "Java Compiler" and make sure the above mentioned option is checked

Eclipse screen

Upvotes: 4

Michael
Michael

Reputation: 351

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);

Upvotes: 23

Related Questions