熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate外置命名查詢報錯

2022-06-13   來源: Java開源技術 

  Hibernate提供了外置命名查詢方式將hql語句放在一個單獨的文件中而不是直接放在程序中

  我的代碼

  [html]

  <class name=Student table=t_student>

  <id name=id column=student_id>

  <generator class=native/>

  </id>

  <property name=name column=student_name/>

  <manytoone name=myclass column=student_class/>

  <query name=serachStu>

  <![CDATA[

  select s from Student s where sid>?

  ]]>

  </query>

  </class>

  <class name=Student table=t_student>

  <id name=id column=student_id>

  <generator class=native/>

  </id>

  <property name=name column=student_name/>

  <manytoone name=myclass column=student_class/>

  <query name=serachStu>

  <![CDATA[

  select s from Student s where sid>?

  ]]>

  </query>

  </class>

  結果在java測試類中執行

  [java]

  List list = sessiongetNamedQuery(serachStu)setParameter( )list();

  List list = sessiongetNamedQuery(serachStu)setParameter( )list();

  出現報錯

  [html]

  orghibernateMappingException: Named query not known: serachStu

  at orghibernateimplAbstractSessionImplgetNamedQuery(AbstractSessionImpljava:)

  ……

  orghibernateMappingException: Named query not known: serachStu

  at orghibernateimplAbstractSessionImplgetNamedQuery(AbstractSessionImpljava:)

  ……

  程序沒有找到這個命名查詢語句這個錯誤的原因是

  在映射文件中配置<query>時應該把他放在<class>的外面

  改成

  [html]

  SPAN >   <class name=Student table=t_student>

  <id name=id column=student_id>

  <generator class=native/>

  </id>

  <property name=name column=student_name/>

  <manytoone name=myclass column=student_class/>

  </class>

  <query name=serachStu>

  <![CDATA[

  select s from Student s where sid>?

  ]]>

  </query></SPAN>

  <class name=Student table=t_student>

  <id name=id column=student_id>

  <generator class=native/>

  </id>

  <property name=name column=student_name/>

  <manytoone name=myclass column=student_class/>

  </class>

  <query name=serachStu>

  <![CDATA[

  select s from Student s where sid>?

  ]]>

  </query>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28671.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.