Hibernate Implementing the Effect of limit Statement

Keywords: PHP Session Hibernate

Hibernate

hibernate implements limit effect

  • Since the limit grammar cannot be written directly in the hql statement, this effect needs to be achieved in other ways.
  • limit effects generally require two parameters: start location and number of queries num. Both parameters are final int
  • The implementation code is as follows:
try {
            //First, create a final hql statement
            final String hql="from xxb where xx="+xx+" order by xx asc ";
            //hibernate performs a lookup with the parameter of the request return method
            List<xx> xxList=getHibernateTemplate().executeFind(new HibernateCallback() {
                public Object doInHibernate(Session session){
                    //Create a lookup object, set the starting location and the number of queries
                    Query query=session.createQuery(hql);
                    query.setFirstResult(start);
                    query.setMaxResults(num);
                    //Convert to list form
                    return query.list();
                }
            });
            return xxList;
        }catch (RuntimeException e){
            throw  e;
        }
  • For the two related methods in the implementation process, the source code can be intercepted and pasted below, or can be viewed by oneself.
    • The executeFind method is implemented as follows:
    public List executeFind(HibernateCallback<?> action) throws DataAccessException { 
          Object result = this.doExecute(action, false, false);
          if (result != null && !(result instanceof List)) {
              throw new InvalidDataAccessApiUsageException("Result object returned from HibernateCallback isn't a List: [" + result + "]");
          } else {
              return (List)result;
          }
      }
    • The doInHibernate method is implemented as follows:
      public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
          Query query = session.createQuery(this.hql).setFirstResult(this.pageModel.getBeginIndex()).setMaxResults(this.pageModel.getPageSize());
          if (!CollectionUtil.listIsNull(this.params)) {
              for(int i = 0; i < this.params.size(); ++i) {
                  Object obj = this.params.get(i);
                  if (obj instanceof String) {
                      query.setString(i, (String)obj);
                  } else if (obj instanceof Integer) {
                      query.setInteger(i, (Integer)obj);
                  } else if (obj instanceof Date) {
                      query.setDate(i, (Date)obj);
                  }
              }
          }
    
          List<T> list = query.list();
          String counthql = "select count(*) " + this.hql;
          Query countQuery = session.createQuery(counthql);
          if (!CollectionUtil.listIsNull(this.params)) {
              for(int i = 0; i < this.params.size(); ++i) {
                  Object obj = this.params.get(i);
                  if (obj instanceof String) {
                      countQuery.setString(i, (String)obj);
                  } else if (obj instanceof Integer) {
                      countQuery.setInteger(i, (Integer)obj);
                  } else if (obj instanceof Date) {
                      countQuery.setDate(i, (Date)obj);
                  }
              }
          }
    
          List<Object> countList = countQuery.list();
          if (CollectionUtil.listIsNull(countList)) {
              this.pageModel.setTotalCount(0L);
          } else {
              this.pageModel.setTotalCount((Long)countList.get(0));
          }
    
          return list;
      }

Posted by gaz_hayes on Sun, 13 Oct 2019 09:07:20 -0700