为什么Spring Data JPA @Query中不支持SpEL表达式?

3

我试图避免将列表大小作为方法的第二个参数传递而产生冗余。相反,我使用EL表达式,但是出现错误:

org.hibernate.QueryException: 未设置所有命名参数: [$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版本中开发的。为什么在我的情况下它不起作用...

1个回答

5
答案很简单:任意表达式未被实现/支持。
请认真查阅Spring Data JPA文档,了解有关使用SpEL表达式的信息。
引用块中提到,自Spring Data JPA 1.4版起,我们支持通过@Query在手动定义的查询中使用受限制的SpEL模板表达式
而支持的表达式仅包括: 变量:entityName 用法:select x from #{#entityName} x 描述:插入与给定存储库相关联的域类型的entityName。entityName解决如下:如果域类型已在@Entity注释上设置name属性,则将使用它。否则,将使用域类型的简单类名。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接