传递多个值。获取org.hibernate.exception.SQLGrammarException:无法提取ResultSet]的根本原因
问题描述:
我正在阅读来自文本文件的查询,并依赖于来自UI的过滤器,使用字符串缓冲区我附加了额外的查询。 。传递多个ID的像上面获取数据传递多个值。获取org.hibernate.exception.SQLGrammarException:无法提取ResultSet]的根本原因
我得到嵌套的例外是javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法提取的ResultSet]与根源 java.sql.SQLSyntaxErrorException:ORA -01722:无效号码。
我正在使用q.setParameter这是Java持久性查询。所以我没有paramterList.Because之间的逗号值我收到无效号码。你能帮我一下,我可以在这里做什么来支持多个值,并可以获取数据。
我的代码示例:
StringBuffer q = new StringBuffer();
q.append(queryFromPropertiesFile);
if(model.getId()!=null && ! model.getId().isEmpty())
q.append(" and emp.id IN (:id)");
Query query = entity.createNativeQuery(q.toString());
query.setParameter("id", model.getId());
答
你需要传递一个List
对象(即拆分使用,
分隔符的字符串),然后通过IDS(List
对象),将setParameter
的该列表如下图所示:
List<String> idsList = new ArrayList<>();
String[] ids = model.getId().split(",");
for(String id : ids) {
idsList.add(id);
}
query.setParameterList("id", idsList);
请参阅 http://*.com/questions/14296234/how-to-use-the-setparameterlist-method-in-hibernate – coolesh
谢谢,但我想用Java.per sistence.Query不是org.hibernate.query.Query。 org.hibernate.query.Query有setParameterList。 – VBR