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>