简体   繁体   中英

Hibernate createNativeQuery using IN clause

Using Java, Hibernate.

I have a query

String pixIds = "1,2,3";
String query = "SELECT * FROM comment WHERE PIX_ID IN (:pixIds)";
q.setParameter("pixIds", pixIds);
List<Object[]> results =  q.getResultList();

I'm not able to bind this parameter to pixIds using the code above. What is the right way to do this?

Note : the query I have here is a simplified version of my actual query.

The following method works
public Query setParameterList(String name, Collection vals) throws HibernateException

Hibernate doesn't support binding collection to IN (...) in SQL queries.

You need to work the same way as with plain JDBC: given a collection, dynamically generate a query with appropriate number of ? s in IN clause, and then bind elements of that collection to ? s.

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