正确写法:
@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();