<query name="com.kkoolerter.beans.Guestbook.selectGuestbook">
<![CDATA[select gb from Guestbook gb where gb.id between:begin and :end]]>
</query>
<query>标签在映射文件中的位置在<hibernate-mapping>标签的<class>标签之后。<query>标签的name属性用来设置命名HQL语句的名称,使用命名的HQL语句如下所示:
public void testNamedHQL(){
Session session = null;
session = HibernateUtils.getCurrentSession();
Query query = session.getNamedQuery("com.kkoolerter.beans.Guestbook.selectGuestbooks");
query.setInteger("begin", 1);
query.setInteger("end", 10);
List<Guestbook> list = query.list();
session.close();
for(Guestbook gb : list){
System.out.println(gb.toString());
}
}