我试图通过将第二个参数传递给具有列表大小的方法来避免冗余。相反,我使用 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 版本中开发的。为什么它在我的情况下不起作用...
最佳答案
答案很简单:未实现/不支持任意表达式。
请仔细检查有关 Using SpEL expressions 的 Spring Data JPA 文档
As of Spring Data JPA release 1.4 we support the usage of restricted SpEL template expressions in manually defined queries via @Query
支持的表达式表只包含
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.
关于java - 为什么 SpEL 支持在 Spring Data JPA @Query 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46834793/