简体   繁体   中英

Using list as a positional parameter in JPA query

I want to know if it's possible to pass in a list as a parameter in native queries. When search up online, an article in Baeldung has exactly what I want to do: Collection-Valued Positional Parameters usage

I did the exact same thing, except that in the article, they used "createQuery" and I used "createNativeQuery". Not sure if this is the reason why mine is not working.

CreateQuery means JPQL was passed in which is parsed and modified into SQL, which allows it to break the collection parameter into its components to pass each into the SQL statement. CreateNativeQuery uses your SQL which isn't modified, and JDBC doesn't understand collections so requires parameters broken up into individual arguments in the SQL. You have to do it yourself and dynamically build the SQL based on the number of parameters in the collection.

There are other questions with solutions that touch on other options, such as using SQL within criteria or JPQL queries that can let you get the best of both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM