Search code examples
javaspringhibernatespring-data-jpaspring-el

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


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


Solution

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