通过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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-11-28
  • 2022-12-23
  • 2021-06-15
  • 2022-12-23
  • 2021-11-12
猜你喜欢
  • 2021-04-28
  • 2021-09-25
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
相关资源
相似解决方案