Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skipping @Param as List in @Query if is null in Spring Data JPA : antlr.NoViableAltException: unexpected AST node

UPD

    @Query(value = "select distinct d.documentId " +
        "from TrainingDocument d " +
        "where (:docTypes is null or d.documentTypeName in :docTypes)")
List<String> findDocByDocTypes(
        @Param("docTypes") List<String> docTypes);

I have a query like above, where I check List<String> docTypes whether it is null or not. With docTypes == null or with only one element in the list it works. As soon as I have more than one element I get:

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: {vector} 
[select distinct d.documentId from com.example.model.TrainingDocument d 
where (:docTypes_0_, :docTypes_1_ is null or d.documentTypeName in
(:docTypes_0_, :docTypes_1_))]; nested exception is
java.lang.IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: {vector} 
[select distinct d.documentId from com.example.model.TrainingDocument d
where (:docTypes_0_, :docTypes_1_ is null or d.documentTypeName in
(:docTypes_0_, :docTypes_1_))]

I already found this solution How to skip @Param in @Query if is null or empty in Spring Data JPA It describes my case but doesn't work for me. I'm using spring-boot 1.5.9.RELEASE

like image 512
Elina Zarisheva Avatar asked Nov 07 '22 11:11

Elina Zarisheva


1 Answers

You can yield null if your vector is empty, or yield the first value if is not

where (coalesce(:docTypes, null) is null or d.documentTypeName in :docTypes)
like image 141
spezzino Avatar answered Nov 15 '22 05:11

spezzino