• 常见Hibernate报错处理:出现“org.hibernate.QueryException: could not resolve property”和 is not mapped和could not locate named parameter错误的解决


      正确写法:

      @Override
        @SuppressWarnings("unchecked")
        public List<Device> queryOSDevice(String cpu){
            String sql = null;
            if(cpu.equals("os_xp")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsXP') ";
            }else if(cpu.equals("os_7")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows7') ";
            }else if(cpu.equals("Os_Win8")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows81') ";
            }else if(cpu.equals("Os_Win10")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows10InsiderPreview') ";
            }else if(cpu.equals("os_vista")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsVista') ";
            }else if(cpu.equals("os_2003")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsServer2003') ";
            }else if(cpu.equals("os_98")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows98') ";
            }else if(cpu.equals("os_95")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows95') ";
            }else if(cpu.equals("os_me")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsMe') ";
            }else if(cpu.equals("os_nt")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsNt') ";
            }else if(cpu.equals("os_2000")){
                sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.Windows2000') ";
            }
            return getSession().createQuery(sql).list();
        }

    1、出现“org.hibernate.QueryException: could not resolve property”错误的解决:

      起初,hql语句是这样写的,报错:could not resolve property:osId

     sql = "from "+this.clazz.getName()+" this WHERE this.osId = (select id from cems_dict_os o where o.name = 'com.vrv.common.system.os.WindowsXP') ";

      考虑需要改为  this.os.id ,但是又出现cems_dict_os is not mapped的错误

    sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from cems_dict_os o where o.name = 'com.vrv.common.system.os.WindowsXP') ";

    2、出现“cems_dict_os is not mapped”错误的解决:

      hql语句都需要从对象里面获取,所以需要将  cems_dict_os  改成它的实体对象  Os

    sql = "from "+this.clazz.getName()+" this WHERE this.os.id = (select id from Os o where o.name = 'com.vrv.common.system.os.WindowsXP') ";

    3、org.hibernate.QueryParameterException: could not locate named parameter [ip]

      其实自己去检查一下SQL语句就可以了

      ①:参数不正确,基本上是" 实体属性=:参数属性 "(尤其是冒号:这个大家会经常丢掉)

      ②:画蛇添足,没有这个参数,自己认为新增的参数,导致找不到

      我导致这个问题的原因是:and this.ip = :ip 写成了 this.ip = ? ;此外注意:  =:参数属性  ;冒号和参数属性之间不能有空格

    4、SSH框架问题——node to traverse cannot be null!报错问题:java.lang.IllegalArgumentException: node to traverse cannot be null!

      这个错误一般是sql、hql语句有问题,比如关键字书写问题,语法问题等,本人犯了一个低级错误,使用update的时候,给多个属性赋值需要使用“,”(逗号)隔开。

    5、org.hibernate.QueryException: Not all named parameters have been set: [name]

    if(!"".equals(name)){
        sql += "and this.name = :name ";
    }
    osDevice = getSession().createQuery(sql).setParameter("name",name).list();

      这样写就会报这个错误,原因是:hibernate 执行sql时没有找到相应的参数名,改为下面这样既可:

    if(!"".equals(name)){
        sql += "and this.name = '" + name + "'";
    }
    osDevice = getSession().createQuery(sql).list();
  • 相关阅读:
    类似直播点赞动画(出现一颗心缓缓升起然后消失)
    进入App弹出提示框
    iOS NSString 截取字符串(根据索引截取)
    刷新tableView 保持头部视图 不变
    截取一段字符串中,两个指定字符串中间的字符串
    ios 导航栏透明, 上下滑动 导航栏 颜色渐变
    ios 自定义键盘的return键以及键盘的其他一些属性
    百度地图通过坐标定位 自己的位置显示小圆点 (精度圈是否显示根据自己喜好) 上图
    百度地图定位 (直接上图上代码)
    iOS 对H5加载html的数据的一些基础设置
  • 原文地址:https://www.cnblogs.com/goloving/p/7683317.html
Copyright © 2020-2023  润新知