예제 #1
0
  public List<T> findInCollectionByPage(
      final String hql, final List value, final int offset, final int pageSize)
      throws HibernateException {

    Query query = getSession().createQuery(hql);
    if (null != value) {
      query.setParameterList("paraList", value);
    }
    if (offset > 1) return query.setFirstResult(offset).setMaxResults(pageSize).list();
    else return query.list();
  }
예제 #2
0
  public Map<String, Object> findByMapWithCond(
      final Map<String, Object> params, int page, int rows, String order, boolean asc)
      throws HibernateException {
    StringBuilder hql = new StringBuilder("from " + className);
    Query query, countQuery;
    Long total;
    Map<String, Object> alterParams = new HashMap<>();
    if (null != params && !params.isEmpty()) {
      hql.append(" where ");
      for (String field : params.keySet()) {
        int i = field.indexOf(" ");
        Assert.isTrue(i != -1, "Wrong condition, must have space inside!");
        String ramdonName = "_" + Utils.getRandomString(8);
        hql.append(field).append(" :").append(ramdonName).append(" and ");
        alterParams.put(ramdonName, params.get(field));
      }
      hql.append("1=1");
      countQuery = getSession().createQuery("SELECT count(*) " + hql.toString());
      if (null != order) {
        hql.append(" order by ").append(order);
        if (asc) hql.append(" asc");
        else hql.append(" desc");
      }
      query = getSession().createQuery(hql.toString());

      for (String field : alterParams.keySet()) {
        query.setParameter(field, alterParams.get(field));
        countQuery.setParameter(field, alterParams.get(field));
      }
    } else {
      countQuery = getSession().createQuery("SELECT count(*) " + hql.toString());
      if (null != order) {
        hql.append(" order by ").append(order);
        if (asc) hql.append(" asc");
        else hql.append(" desc");
      }
      query = getSession().createQuery(hql.toString());
    }
    total = (Long) countQuery.uniqueResult();
    if (page > 0 && rows > 0) {
      query.setFirstResult((page - 1) * rows).setMaxResults(rows);
    }
    Map<String, Object> result = new HashMap<String, Object>();
    result.put("total", total);
    result.put("rows", query.list());
    return result;
  }