【转】关于Spring Hibernate 取数据问题

通过getHibernateTemplate()的方法来取几条数据开始一直没有解决好。

开始的时候通过getHibernateTemplate().setMaxResult(num)的方式来设置要得到的数据的行数。结果这个用了之后导致用getHibernateTemplate().find() 取出来的都是 num 条数据。开始没有意识到这个问题,结果绕了一个大弯,还以为其他地方写错了。

要取所有的时候,没办法用getHibernateTemplate().setMaxResult(0) 试了一下,取到数据,问题貌似解决了,不过觉得getHibernateTemplate().setMaxResult(num)这样来设置不是很好。

于是上网查资料,找到一个写好的方法,修改一下就Ok

@SuppressWarnings("unchecked")
public List<T> find(final String hsql, final int firstRow, final int maxRow)
    throws Exception {
   return getHibernateTemplate().executeFind(new HibernateCallback() {
    public Object doInHibernate(Session s) throws HibernateException,
      SQLException {
     Query query = s.createQuery(hsql);
     query.setFirstResult(firstRow);
     query.setMaxResults(maxRow);
     List list = query.list();
     return list;
    }
   });
}

 

原文地址:http://hi.baidu.com/a5423804/blog/item/4b168efb51894f116d22eb92.html

posted on 2011-05-07 17:25  yingql  阅读(528)  评论(0编辑  收藏  举报

导航