显斌的技术Blog

君子博学而日参省乎己......

实现Hibernate 分页的一段代码

       /**
        * @author 苏显斌
        *
        * 获取指定范围的数据库记录。
        * @param helpctyId
        *            帮助分类唯一标识
        * @param startIndex
        *            开始位置
        * @param endIndex
        *            结束位置
        * @return 帮助明细对象列表
        */
       public HelpDetail[] queryDetails(int startIndex,
                     int endIndex) throws HibernateException, MessageException {
              if (startIndex < 0 || endIndex < 0 || (endIndex - startIndex) < 0)
                     throw new MessageException("记录的起始或结束索引无效!");
 
              Session session = HibernateUtil.currentSession();
              Transaction tx = session.beginTransaction();
 
              List list = null;
              try {
                     Query q = session.createQuery("from HelpDetail as detail");
                     q.setFirstResult(startIndex);
                     q.setMaxResults(endIndex - startIndex);
                     list = q.list();
                     tx.commit();
              } finally {
                     HibernateUtil.closeSession();
              }
             
              if (list == null || list.size() <= 0)
                     return new HelpDetail[0];
             
              HelpDetail details[] = new HelpDetail[list.size()];
              for (int i = 0; i < list.size(); i++) {
                     details[i] = (HelpDetail) list.get(i);
              }
             
              return details;
       }
阅读更多
个人分类: Java 技术专题
上一篇Required Java Version:1.4.1. Availible:1.3.1_01
下一篇Using Clobs/Blobs with Oracle and Hibernate(Hibernate中使用Oracle的Clobs/Blobs类型)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭