常見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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM