JPA passing list to IN clause in named native query

前端 未结 10 1085

I know I can pass a list to named query in JPA, but how about NamedNativeQuery? I have tried many ways but still can\'t just pass the list to a NamedNativeQuery. Anyone know

10条回答
  •  甜味超标
    2020-11-30 07:52

    The above accepted answer is not correct and led me off track for many days !!

    JPA and Hibernate both accept collections in native query using Query.

    You just need to do

    String nativeQuery = "Select * from A where name in :names"; //use (:names) for older versions of hibernate
    Query q = em.createNativeQuery(nativeQuery);
    q.setParameter("names", l);
    

    Also refer the answers here which suggest the same (I picked the above example from one of them)

    1. Reference 1
    2. Reference 2 which mentioned which cases paranthesis works which giving the list as a parameter

提交回复
热议问题