问题描述
我处理Spring Data JPA,并且有一个实现JpaRepository的存储库接口.
I work on Spring Data JPA and I have a repository interface which implements JpaRepository.
我已经编写了此查询,该查询非常有效:
I have already written this query which works perfectly:
@Query ("FROM Person p " +
"LEFT JOIN p.relatedContractRoleAttributions rcras " +
"WHERE rcras.contract.id = :#{#contract.id} " +
"AND rcras.relatedContractRole.code = :#{#code}")
Person findByContractAndRelatedContractRole(@Param ("contract") Contract contract, @Param ("code") String code);
现在,我想编写另一个可以在多个代码中找到的查询,所以我写了这个查询:
Now I want to write another query which can find in more than one code so I wrote this query:
@Query ("FROM Person p " +
"LEFT JOIN p.relatedContractRoleAttributions rcras " +
"WHERE rcras.contract.id = :#{#contract.id} " +
"AND rcras.relatedContractRole.code IN (:#{#codes})")
List<Person> findByContractAndRelatedContractRoles(@Param ("contract") Contract contract, @Param ("codes") String... codes);
但是当我启动我的应用程序时,出现此错误:
But when I start my application I have this error:
Caused by: org.hibernate.QueryException: unexpected char: '#' [FROM com.krgcorporate.core.domain.access.Person p LEFT JOIN p.relatedContractRoleAttributions rcras WHERE rcras.contract.id = :#{#contract.id} AND rcras.relatedContractRole.code IN (:__$synthetic$__2)]
你知道为什么吗?
感谢您的帮助.
推荐答案
我认为org.springframework.data.jpa.repository.query.StringQuery中存在问题. spring-data-jpa-1.7.2.RELEASE(第250..259行)
I believe that there is an issue in org.springframework.data.jpa.repository.query.StringQuery. spring-data-jpa-1.7.2.RELEASE (lines 250..259)
case IN:
if (parameterIndex != null) {
checkAndRegister(new InParameterBinding(parameterIndex, expression), bindings);
} else {
checkAndRegister(new InParameterBinding(parameterName, expression), bindings);
}
result = query;
break;
因此,当StringQuery用SPEL绑定"in"参数时,它将用@Query批注中的字符串覆盖结果查询字符串.然后用新的绑定替换'in'SPEL.
So when StringQuery binds 'in' parameter with SPEL it overrides the result query string with the string in your @Query annotation. And then it replaces 'in' SPEL with the new binding.
如果您更改
"WHERE rcras.contract.id = :#{#contract.id} " +
"AND rcras.relatedContractRole.code in :#{#code}"
进入
"WHERE rcras.relatedContractRole.code in :#{#code}" +
"AND rcras.contract.id = :#{#contract.id} "
它将解决您的问题
更新:SpingDataJpa团队已修复此问题. https://jira.spring.io/browse/DATAJPA-712
UPDATE:SpingDataJpa team has fixed it. https://jira.spring.io/browse/DATAJPA-712
这篇关于在IN查询中使用Spel的Spring Data JPA @Query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!