我正在尝试从数据库迭代两个集合中获得结果,如下所示
for (SomeType element1 in List1) {
for (SomeType2 element2 in List2) {
Query query = session.createQuery("from Table where column1 =: element1 and column2 =: element2");
//Operations on list of results
}
}
如何以最佳性能执行此类操作?在我的应用程序中,list1有200个元素,list2大约有30个,需要花费太多时间。
答案 0 :(得分:1)
您可以尝试:
Query query = session.createQuery("from Table where column1 IN (:List1) and column2 IN (:List2)");
答案 1 :(得分:0)
不完美,但它可以完成这项工作:
StringBuilder sb = new StringBuilder("(");
for (SomeType element1 in List1) {
for (SomeType2 element2 in List2) {
sb.append(element1).append(',').append(element2).append("),");
}
}
// delete the last ','
sb.setLength(sb.length() - 1);
Query query = session.createQuery(
"from Table where (column1, column2) in ("+sb.toString+")"
);