MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » in thread "main" org.hibernate.Mapping

in thread "main" org.hibernate.MappingException:

www.MyException.Cn  网友分享于:2013-11-10  浏览:2803次
关于映射文件中设置HQL的问题
public class Test {
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
  Query q=session.getNamedQuery("chaxun_name");
q.setString(0,"20");//给第一个参数传20
Iterator it=q.list().iterator();
if(it.hasNext()){
System.out.println(it.next()); 
}
tx.commit();
session.close();

hbm.xml文件
<hibernate-mapping>
  <class name="com.hibernate.Student" table="student" catalog="hibernate01">
  <id name="sid" type="java.lang.Integer">
  <column name="sid" />
  <generator class="identity" />
  </id>
  <property name="sname" type="java.lang.String">
  <column name="sname" length="250" />
  </property>
  <property name="sage" type="java.lang.Integer">
  <column name="sage" />
  </property>
  <property name="sclass" type="java.lang.Integer">
  <column name="sclass" />
  </property>
  <query name="chaxun_name">
  select count(sage) from Student as stu where stu.sage>?
  </query>
  </class>
</hibernate-mapping>

报错:
Exception in thread "main" org.hibernate.MappingException: Named query not known: chaxun_name
at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:70)
at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1260)
at com.hibernate.Test.main(Test.java:108)


------解决方案--------------------
你的query的标签写的位置不对,要放在class标签的外面,hibernate-mappings标签的里面。
------解决方案--------------------
query的标签要放在class标签的外面

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有