Hibernate 学习笔记第四篇
Hibernate 查询语言之HQL
命名查询:
<!-- 类似于iBatis 中的以key 找value -->
<query name="findDept" >
<![CDATA[from Department d where d.id=:OID ]]>
</query>
@Test
public void testSave(){
Session session = sessionFactory.openSession();
Transaction tran = session.beginTransaction();
tran.begin();
Department dep = (Department) session.getNamedQuery("findDept").setLong("OID",Long.valueOf(1)).uniqueResult();
System.out.println(dep.getName());
session.close();
}
一定要加上uniqueResult();
Hibernate:
select
department0_.id as id0_,
department0_.name as name0_,
department0_.description as descript3_0_
from
dept department0_
where
department0_.id=?
研发部