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

主宰稳场 提交于 2020-02-24 12:18:10

问题


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: 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);

}

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...


回答1:


The answer is simple: arbitrary expressions are not implemented/supported.

Please check carefully on Spring Data JPA documentaiton regarding Using SpEL expressions

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

And the table of supported expressions contains only

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.



来源:https://stackoverflow.com/questions/46834793/why-spel-support-doesnt-work-in-spring-data-jpa-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!