(1)Hibernate中Query.uniqueResult();
如果有多个值抛错
如果有值且只有一个,返回一个object
如果没值,返回null
(2)setEntity(0,organization) 是把organization的值插入到HQL中的第一个?中,是实体类型
其他的如:setString(1,name)name是传过来到参数
分页查询
java 代码
- public PageList findPreEmployeesByOrganization(
- final Organization organization, final PageRequest pageRequest,
- final SortInfo sortInfo)
- {
- HibernateCallback callBack = new HibernateCallback()
- {
- public Object doInHibernate(Session session)
- throws HibernateException, SQLException
- {
- String queryString = "select distinct e from Employee e join e.posts pe join pe.post p join
- p.organizations o where o.organization.organizationId = ? and e.postStatus = 2"
- + sortInfo.toString();
- List list = session.createQuery(queryString).setEntity(0,
- organization).setFirstResult(pageRequest.getStartNum())
- .setMaxResults(pageRequest.getPagesize()).list();
- String countQueryString = "select count(distinct e) from Employee e join e.posts pe join
- pe.post p join p.organizations o where o.organization.organizationId = ? and e.postStatus = 2";
- int recCount = ((Integer) session.createQuery(countQueryString)
- .setEntity(0, organization).uniqueResult()).intValue();
- PageList pageList = new PageList(list, pageRequest
- .getPagesize(), recCount, pageRequest.getPageno());
- return pageList;
- }
- };
- return (PageList) getHibernateTemplate().execute(callBack);
- }