常见Hibernate报错处理:出现“org.hibernate.QueryException: could not resolve property”和 is not mapped和could no...

  正确写法:

  @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();

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值