Hibernate createNativeQuery using IN clause

大憨熊 提交于 2019-12-02 05:30:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!