• spring+hibernate中的Result object returned from HibernateCallback isn't a List


    Ok the problem is that for executeFind() the return type is List....so there is no way to use uniqueResult() within the callback from executeFind()...may be we should use execute()

    上面这段话来自http://forum.springframework.org/showthread.php?t=58370

    在使用executeFind方法时,如果返回的不是List类型的数据则会出现引异常

    例:
     public Integer findCount(final Class clazz,final String str) {
      return Integer.parseInt(getHibernateTemplate().executeFind(new HibernateCallback()
      {

       @Override
       public Object doInHibernate(Session session)
         throws HibernateException, SQLException {
        StringBuilder sb=new StringBuilder();
        sb.append("select count(*) from ");
        sb.append(clazz.getName());
        sb.append(" ");
        sb.append(str);
        log.info(sb.toString());
        return session.createQuery(sb.toString()).uniqueResult();
       }
       
      }).toString());
     }

    解决方法是把executeFind方法改为execute方法

     public Integer findCount(final Class clazz,final String str) {
      return Integer.parseInt(getHibernateTemplate().execute(new HibernateCallback()
      {

       @Override
       public Object doInHibernate(Session session)
         throws HibernateException, SQLException {
        StringBuilder sb=new StringBuilder();
        sb.append("select count(*) from ");
        sb.append(clazz.getName());
        sb.append(" ");
        sb.append(str);
        log.info(sb.toString());
        return session.createQuery(sb.toString()).uniqueResult();
       }
       
      }).toString());
     }

  • 相关阅读:
    mysql 15道语句练习题
    分组查询以及where和having的区别
    java初学复习
    Working with Excel Files in Python
    PIP常用命令
    pip install 提示代理连接失败原因及解决办法
    关于Encode in UTF-8 without BOM
    360极速浏览器Onetab插件存储位置
    使用夜神模拟器录制脚本
    微信小程序开发经验总结
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/4273079.html
Copyright © 2020-2023  润新知