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

标签 java spring hibernate spring-data-jpa spring-el

我试图通过将第二个参数传递给具有列表大小的方法来避免冗余。相反,我使用 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/

相关文章:

Java Google App Engine 数据存储区放置/获取

spring - 为什么Spring在application/json get请求上解码+(加号字符)?我该怎么办?

java - Spring boot sessionRegistry 返回空的主体列表

java - hibernate "auto schema update"是否会丢弃它不拥有的任何内容?

java - 由于 hibernate 中的重复实体而引发异常

java - Spring Data JPA 存储库 findAll() 空指针

java - 用户 facebook "access token"是永久且唯一的吗?

java - 尝试实现 Primefaces 示例时出错

java - 在java中,使引用变量可变,是否也会使其所有字段也可变?

java - Spring Integration 通过控制总线手动启动/停止 channel 适配器