programstudy0821 2012-09-01 09:06
浏览 626
已采纳

getHibernateTemplate()用法

public List findByDeviceIdsRasId(final String rasId, final List deviceIds)
throws DataAccessException
{
if (StringUtils.isEmpty(rasId) || CollectionUtils.isEmpty(deviceIds))
{
return null;
}
/*return getHibernateTemplate().execute(new HibernateCallback>()
{

        @Override
        public List<RasPo> doInHibernate(Session session) throws HibernateException,
                SQLException
        {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("(");
            for (String deviceId : deviceIds)
            {
                stringBuffer.append("'").append(deviceId).append("'").append(",");
            }
            String str = stringBuffer.substring(0, stringBuffer.length() - 1);
            str = str + ")";
            Query query= session.createQuery("from RasPo where rasId='" + rasId + "' and deviceId in " + str);
            List<RasPo> list = query.list();
            return list;
        }
    });*/
    List<RasPo> list=null;
    try{
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("(");
        for (String deviceId : deviceIds)
        {
            stringBuffer.append("'").append(deviceId).append("'").append(",");
        }
        String str = stringBuffer.substring(0, stringBuffer.length() - 1);
        str = str + ")";
        list = getHibernateTemplate().find("from RasPo where rasId=? and deviceId in ?",
                new String[] {rasId, str});
    }
    catch (DataAccessException e)
    {
        throw e;
    }
    return list;
}

使用上面的注释的可以查询出来,但是改为find()方法后就查询不出来了。我感觉着没有什么区别了。求解

  • 写回答

3条回答 默认 最新

  • jinnianshilongnian 2012-09-01 09:53
    关注

    ist = getHibernateTemplate().find("from RasPo where rasId=? and deviceId in ?",
    new String[] {rasId, str});

    第二个?是 in 不能传 一个字符串,解决方案,类似于下边:

    List result = getHibernateTemplate().executeFind(new HibernateCallback>() {
    @Override
    public List doInHibernate(Session session) throws HibernateException, SQLException {

                String hql = "from SampleModel where uuid in (:uuids)";
    
                Query q = session.createQuery(hql);
                q.setParameterList("uuids", new Object[]{0, 1}); 
                return q.list();
            }
    
        });
    

    1、hql使用 String hql = "from SampleModel where uuid in (:uuids)"; 在hibernate必须使用命名参数;
    2、赋值 q.setParameterList("uuids", new Object[]{0, 1}); 可以是集合/数组;

    hibernate会动态生成如下语句(in里边多个?)
    Hibernate: select samplemode0_.uuid as uuid0_, samplemode0_.name as name0_ from tbl_sample samplemode0_ where samplemode0_.uuid in (? , ?)

    如下是不支持的
    list = getHibernateTemplate().find("from RasPo where rasId=? and deviceId in ?",

    new String[] {rasId, str});

    你的也应该改成类似于这样
    hql = "from RasPo where rasId=:rasId and deviceId in (:deviceIds)";

    设置参数
    不能传str 而是一个deviceIds集合

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败