菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
379
0

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

原创
05/13 14:22
阅读数 93807

  正确写法:

  @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/200
379 点赞
0 评论
收藏
为你推荐 换一批