标题我也不知道怎么起才好,但是我描述一下我的功能场景:
我用数组接到组参数值,在hibernate 我准备用 IN  作为where条件,
代码如下:
String strSql = " FROM DPourRecord WHERE wheel_code  in (?)";
List paramsList = new ArrayList();
paramsList.add(paramsStr);
try {
list = getHibernateTemplate().find(strSql,paramsList.toArray());
System.out.println("*******"+list.size());
} catch (RuntimeException e) {
e.printStackTrace();
log.error("find wheels by wheelcodes is failed!");
}但是这样就成功不了,后来我把数组的值拼接了一个类型这样的字符串:
in ('12','13','14')
也就是说:把数组组合成这样的  '12','13','14'形式,程序不出错了,但是查询不出结果来,如果将sql 直接通过sql 界面工具 执行,比如 
select * from tableName where colName1 in ('12','13','14')   就能查找出正确的结果集请各位大侠帮助一下!另外,在使用spring  hibernate 过程中批量 更新和插入,大家有没有好的建议,在网上大家都不推荐使用hibernate 去做批量操作,从效率讲,不如直接使用jdbc ,可是如果抛开效率的话,(因为我本身的批量数据量也不会太大),大家有好的解决方案可以供我借鉴和参考的嘛?谢谢大家了!欢迎赐教!

解决方案 »

  1.   

    array是个数组,不能这样赋值。
    数组组合成str = '12','13','14'; 然后setString(1,str)同样查不出值,因为数据库把'12','13','14'当作一个字符串来处理,比如oracle会把'12','13','14'在你调用的时候转为 ''12'',''13'',''14'',这就是为什么我们说PreparedStatement能防止sql注入的原因。
    批量操作不知道hib会否提供executeBatch()这样的功能,如果没有,还是用jdbc的快这是肯定的。
    你的批量数据不大的话,for循环拉倒吧呵呵
      

  2.   

    List paramsList = new ArrayList(); 
    ->
    List<String> paramsList = new ArrayList(); paramsList.toArray()
    ->
    paramsList.toArray(new String[]{})
      

  3.   

    HQL支持 in list 这样的形式。
      

  4.   

    上面三位说的都挺好,不过我还是没把我的问题解决了,能请哪位给详细的解释一下,比如三楼的提到,hql支持 in  list 这样的形式,那能否详细的说一下啊?还有二楼的,我按照你的球修改,可是还是执行不了呢!一楼的解释问题很详细,我也是恍然大悟,不过没有说我怎么去解决我这个问题。谢谢各位了,请帮帮吧!!!!
      

  5.   

    楼主,hib我不懂的。但是批量插入,我可以给你个PreparedStatement的例子,我这个是读文件的,相信你看得懂:public static void readFileWriteBack(String fileName, Connection con)
    throws Exception {
    String s = "";
    File f = new File(fileName);
    if (!f.exists()) {
    throw new Exception("file doesn't exist....");
    }
    BufferedReader br = null;
    PreparedStatement pstmt = null;
    try {
    br = new BufferedReader(new InputStreamReader(
    new FileInputStream(f)));
    String sql = "insert into tbl_report(单价,工号,统计量,日期)"
    + "values(?,?,?,'2009-12-07')";
    pstmt = con.prepareStatement(sql);
    // 循环外部准备好prepareStatement;
    while ((s = br.readLine()) != null) {
    if (s.indexOf("合计") > 0) {
    continue;
    }
    String[] c = s.split("\t");
    printArray(c);
    if (c.length == 3) {
    // 加入批量参数
    pstmt.setString(1, c[0]);
    pstmt.setString(2, c[1]);
    pstmt.setString(3, c[2]);
    pstmt.addBatch();
    }
    }
    // 一次执行。
    pstmt.executeBatch();
    System.out.println("file write back finished");
    } catch (Exception e) {
    throw e;
    } finally {
    try {
    if (pstmt != null) {
    pstmt.close();
    }
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  6.   

    hql--> HGetDocs
    select A from com.po.TDocument A where A.docId in (:ids) 
    调用的时候:
    List<Integer>lst=初始化一个集合。
    query=getSession().getNamedQuery("HGetDocs");
    query.setParameterList("ids", lst);
      

  7.   

    做开发的朋友,请保护好自己的眼睛。 
    http://shop58425488.taobao.com/
      

  8.   

    除了这样的参数以外,其他的sql都能正常执行么?
    这个我用过,应该是没有问题的。
    8楼的方法你可一试。