【问题标题】:Custom SQL query in JPA repositoriesJPA 存储库中的自定义 SQL 查询
【发布时间】:2017-03-30 15:13:20
【问题描述】:

对于以下查询:

@Query("SELECT fd FROM DPPFile fd WHERE fd.ID= ':ID'")
List<DPPFile> findByID(@Param("ID")String ID/);

我收到以下奇怪的异常:

Caused by: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
    at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:502)
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:692)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:181)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:140)
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:61)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:100)
    at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.bind(SpelExpressionStringQueryParameterBinder.java:69)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:160)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:151)
    at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateQuery(AbstractStringBasedJpaQuery.java:81)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:190)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:121)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:85)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)

如果我使用 ?1 而不是命名参数方法,我预计会出现此异常。使用 ?1 也失败了,所以我不确定这里出了什么问题。 DPPFile 类有一个名为 ID 的成员以及 getter/setter。

(我没有使用默认的“findByID”而没有在其上进行查询的原因是因为我想使用附加参数扩展查询)

【问题讨论】:

  • 为什么要把参数放在单引号里?这意味着它被解释为 STRING ':ID'
  • 啊啊!就是这样。 Pfff,我已经研究了一个小时这个问题,它一直在我的眼前。谢谢,这是正确的答案

标签: hibernate jpa spring-boot


【解决方案1】:

不要将参数放在单引号中,否则它会被解析为文字。因此查询不会被注册。

【讨论】:

    【解决方案2】:

    作为替代解决方案,您可以使用 spring-data findOne 跳过整个 @Query 注释,假设 ID 是您在实体中的唯一标识符。否则,您可以依靠 spring-data 从方法名称生成查询。使用这样的东西:

    @Entity
    public class DPPFile {
    
      @Id
      private Integer identifier;
    
      private Integer ID;
    }
    
    public interface DPPFileRepository extends Repository< DPPFile, Integer> {
    
       List<DPPFile> findByID(String ID);
    }
    

    https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2019-06-17
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多