论坛首页 Java企业应用论坛

关于分页的问题

浏览 4534 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-09-28  
作分页的时候 begin 每次加10再传进来,可是我怎么才能知道什么时候数据已经取完了,不用再传begin了?
         public List getItemListInCategory(Long category_id,int begin); throws HibernateException{
    Session session = sf.openSession();;
    Transaction tx = null;
    List items = null;
    try{
        tx = session.beginTransaction();;
        Content_category category = (Content_category); session.load(Content_category.class,category_id);;
        Query q = session.createQuery("select icm.item"+
                                      "from Item_category_map as icm "+
                                      "where icm.category=:category");;
        q.setParameter("category",category);;
        q.list();;
        q.setFirstResult(begin);;
        q.setMaxResults(10);;
        items = q.list();;
        tx.commit();;
    }
    catch(HibernateException e);{
      if(tx!=null); tx.rollback();;
        throw e;
    }
    finally{
      session.close();;
    }
    return items;
  }
   发表时间:2003-09-28  
查询出总记录数。

不过你的查询hql挺奇怪的,不知道你是如何映射的?
0 请登录后投票
   发表时间:2003-09-29  
我还是不大明白,如果查询记录总数的话,相当于我已经可以把所有的数据的可以取到list中了,那我就用不到hibernate提供的分页功能了?
0 请登录后投票
   发表时间:2003-09-29  
摘录自hibernate 文档第9章。
9.13. Tips & Tricks
You can count the number of query results without actually returning them: 

( (Integer); session.iterate("select count(*); from ....");.next(); );.intValue();
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics