使用 @Param 作为查询方法参数,或者在 Java 8+ 上使用 javac 标志 -parameters

Ish*_*ary 12 java hibernate

我的本地系统上运行以下代码:

@Query(value = "select l from TnncSetlmtSvcGrpStagingEntity l where l.runDate >=:runDate")
List<TnncSetlmtSvcGrpStagingEntity> findAllByRunDateGT(Date runDate);
Run Code Online (Sandbox Code Playgroud)

但是当我在更高的环境中运行同一段代码时,我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

我检查了服务器上的java版本:

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)
Run Code Online (Sandbox Code Playgroud)

在我的本地系统上,我也在使用 Java 8。

谁能帮我解决这个问题。任何帮助表示赞赏。

小智 18

您是否尝试过使用命名参数?

6.3.6。使用命名参数 默认情况下,Spring Data JPA 使用基于位置的参数绑定,如前面所有示例中所述。这使得查询方法在重构参数位置时容易出错。为了解决这个问题,可以使用@Param注解给方法参数一个具体的名称,并在查询中绑定该名称

有关详细信息,请参阅https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.named-parameters。

所以,这应该有效:

@Query(value = "select l from TnncSetlmtSvcGrpStagingEntity l where l.runDate >=:runDate")
List<TnncSetlmtSvcGrpStagingEntity> findAllByRunDateGT(@Param("runDate") Date runDate);
Run Code Online (Sandbox Code Playgroud)

  • 为什么它在我的本地环境中运行良好,而在其他环境中却运行不佳? (3认同)