关于映射文件中设置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标签的外面