public void initialize(Serializable key, SessionImplementor session) throws HibernateException {

    LOG.debugf("Initializing collection: %s using named query: %s", persister.getRole(), queryName);

    // TODO: is there a more elegant way than downcasting?
    AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedSQLQuery(queryName);
    if (query.getNamedParameters().length > 0) {
      query.setParameter(query.getNamedParameters()[0], key, persister.getKeyType());
    } else {
      query.setParameter(0, key, persister.getKeyType());
    }
    query.setCollectionKey(key).setFlushMode(FlushMode.MANUAL).list();
  }
  /**
   * 根据分页请求参数与Query获取分页请求对象
   *
   * @param request 分页请求参数对象
   * @param query Hibernate Query
   * @return {@link Page}
   */
  public <X> Page<X> findPage(PageRequest request, Query query) {
    Page<X> page = new Page<X>(request);

    if (request == null) {
      return page;
    }

    AbstractQueryImpl impl = (AbstractQueryImpl) query;

    String queryString = setPageRequestToHql(impl.getQueryString(), request);
    ReflectionUtils.setFieldValue(impl, "queryString", queryString);

    if (request.isCountTotal()) {
      long totalCount = 0;

      if (impl.hasNamedParameters()) {
        Map<String, TypedValue> map = ReflectionUtils.getFieldValue(impl, "namedParameters");
        Map<String, Object> values = new HashMap<String, Object>();

        for (Map.Entry<String, TypedValue> entry : map.entrySet()) {
          values.put(entry.getKey(), entry.getValue().getValue());
        }

        totalCount = countHqlResult(impl.getQueryString(), values);

      } else {
        List<Object> values = ReflectionUtils.invokeGetter(impl, "values");
        totalCount = countHqlResult(impl.getQueryString(), values.toArray());
      }

      page.setTotalItems(totalCount);
    }

    impl.setFirstResult(request.getOffset());
    impl.setMaxResults(request.getPageSize());

    List result = impl.list();
    page.setResult(result);

    return page;
  }
 private int countGeneratedParameters(Query query) {
   AbstractQueryImpl hqlQueryImpl = (AbstractQueryImpl) query;
   return hqlQueryImpl.getParameterMetadata().getNamedParameterNames().size();
 }