为什么 SpEL 支持在 Spring Data JPA @Query 中不起作用?

Why SpEL support doesn't work in Spring Data JPA @Query?

我试图通过将第二个参数传递给具有列表大小的方法来避免冗余。相反,我使用 EL,但出现错误:

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 版本中开发的。为什么它对我不起作用...

答案很简单:任意表达式都不是implemented/supported。

请仔细检查有关 Using SpEL expressions

的 Spring 数据 JPA 文档

As of Spring Data JPA release 1.4 we support the usage of restricted SpEL template expressions in manually defined queries via @Query

而支持的表达式table仅包含

Variable: entityName

Usage: select x from #{#entityName} x

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.