为什么SpEL支持在Spring Data JPA @Query中不起作用? [英] Why SpEL support doesn't work in Spring Data JPA @Query?
问题描述
我试图通过将第二个参数传递给具有列表大小的方法来避免冗余.相反,我使用EL,但出现错误:
I'm trying to avoid redundancy with passing second argument to method with list size. Instead, I use EL, but I have an error:
org.hibernate.QueryException:尚未设置所有命名参数: [ $ synthetic $ __ 1] [从书本b中选择不同的书本b加入书本b.bookHashtags 作为ht,其中b.uniqueIdentifier具有:tags组中的ht.hashtagName count(ht.uniqueIdentifier)=: $ synthetic $ __ 1]
org.hibernate.QueryException: Not all named parameters have been set: [$synthetic$__1] [SELECT distinct b FROM Book b join b.bookHashtags as ht where ht.hashtagName in :tags group by b.uniqueIdentifier having count(ht.uniqueIdentifier) = :$synthetic$__1]
@Repository
public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> {
@Query("SELECT distinct b FROM Book b join b.bookHashtags as ht where ht.hashtagName in :tags " +
"group by b.uniqueIdentifier having count(ht.uniqueIdentifier) = :#{#tags.size()}")
List<Book> findAllBooksContainedTags(@Param("tags") Set<String> tags);
}
我使用spring-data-jpa 1.11.0.RELEASE.我知道此功能是在1.4版本中开发的.为什么在我的情况下不起作用...
I use spring-data-jpa 1.11.0.RELEASE. I know that this feature was developed in 1.4 release. Why it doesn't work in my case...
推荐答案
答案很简单:没有实现/不支持任意表达式.
The answer is simple: arbitrary expressions are not implemented/supported.
请仔细检查有关使用SpEL表达式
从Spring Data JPA版本1.4开始,我们支持在通过@Query手动定义的查询中使用受限制的SpEL模板表达式
受支持的表达式表仅包含
And the table of supported expressions contains only
变量:entityName
Variable: entityName
用法:从#{#entityName} x
Usage: select x from #{#entityName} x
描述:插入与给定存储库关联的域类型的entityName.对EntityName的解析如下:如果域类型在@Entity批注上设置了名称属性,则将使用它.否则,将使用域类型的简单类名.
Description: Inserts the entityName of the domain type associated with the given Repository. The entityName is resolved as follows: If the domain type has set the name property on the @Entity annotation then it will be used. Otherwise the simple class-name of the domain type will be used.
这篇关于为什么SpEL支持在Spring Data JPA @Query中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!