问题
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.
回答1:
The following method works
public Query setParameterList(String name, Collection vals) throws HibernateException
回答2:
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.
来源:https://stackoverflow.com/questions/4878133/hibernate-createnativequery-using-in-clause