Package org.hibernate

Examples of org.hibernate.Query


   * @param parameters
   * @return
   */
  private List doDynamicQuery(final Pagination pagination,
      final Map parameters, final int scope) {
    Query query = null;

    // 是否需要分页
    final boolean needToPage = pagination != null;
    int recordAmount = 0;
    if (needToPage) {// 初始化分页信息
      // 构造动态查询的统计SQL语句
      query = this.session.createQuery(this.buildDynamicSQL(parameters,
          true, scope));
      // 处理参数
      this.fittingQuery(query, parameters);
      // 获取记录总数
      recordAmount = Integer.parseInt(query.uniqueResult().toString());
    }

    // 构造动态查询的统计SQL语句
    query = this.session.createQuery(this.buildDynamicSQL(parameters,
        false, scope));

    // 数据分页处理
    if (needToPage) {
      this.pagingQuery(query, pagination);
    }

    // 处理参数
    this.fittingQuery(query, parameters);

    // 获取查询结果
    final List result = query.list();

    // 返回查询结果
    if (needToPage) {
      Page<E> page = null;
      if (recordAmount == 0) {
View Full Code Here


      super();
    }

    private static Query buildHibernateQuery(org.beangle.model.query.Query<?> bquery,
        final Session hibernateSession) {
      Query hibernateQuery = null;
      if (bquery.getLang().equals(org.beangle.model.query.Lang.HQL)) {
        hibernateQuery = hibernateSession.createQuery(bquery.getStatement());
      } else {
        hibernateQuery = hibernateSession.createSQLQuery(bquery.getStatement());
      }
      if (bquery.isCacheable()) {
        hibernateQuery.setCacheable(bquery.isCacheable());
      }
      setParameter(hibernateQuery, bquery.getParams());
      return hibernateQuery;
    }
View Full Code Here

     */
    public static int count(final org.beangle.model.query.LimitQuery<?> limitQuery,
        final Session hibernateSession) {
      final org.beangle.model.query.Query<?> cntQuery = limitQuery.getCountQuery();
      if (null == cntQuery) {
        Query hibernateQuery = buildHibernateQuery(limitQuery, hibernateSession);
        return hibernateQuery.list().size();
      } else {
        Query hibernateQuery = buildHibernateQuery(cntQuery, hibernateSession);
        final Number count = (Number) (hibernateQuery.uniqueResult());
        if (null == count) {
          return 0;
        } else {
          return count.intValue();
        }
View Full Code Here

    @SuppressWarnings("unchecked")
    public static <T> List<T> find(final org.beangle.model.query.Query<T> query,
        final Session hibernateSession) {
      if (query instanceof LimitQuery<?>) {
        LimitQuery<T> limitQuery = (LimitQuery<T>) query;
        Query hibernateQuery = buildHibernateQuery(limitQuery, hibernateSession);
        if (null == limitQuery.getLimit()) {
          return hibernateQuery.list();
        } else {
          final PageLimit limit = limitQuery.getLimit();
          hibernateQuery.setFirstResult((limit.getPageNo() - 1) * limit.getPageSize())
              .setMaxResults(limit.getPageSize());
          return hibernateQuery.list();
        }
      } else {
        return buildHibernateQuery(query, hibernateSession).list();
      }
    }
View Full Code Here

    query.setFirstResult((limit.getPageNo() - 1) * limit.getPageSize())
        .setMaxResults(limit.getPageSize());
    List<T> targetList = query.list();

    String queryStr = buildCountQueryStr(query);
    Query countQuery = null;
    if (query instanceof SQLQuery) {
      countQuery = getSession().createSQLQuery(queryStr);
    } else {
      countQuery = getSession().createQuery(queryStr);
    }
    QuerySupport.setParameter(countQuery, params);
    // 返回结果
    return new SinglePage<T>(limit.getPageNo(), limit.getPageSize(),
        ((Number) (countQuery.uniqueResult())).intValue(), targetList);
  }
View Full Code Here

  public <T> T get(String entityName, Serializable id) {
    if (StringUtils.contains(entityName, '.')) {
      return (T) getHibernateTemplate().get(entityName, id);
    } else {
      String hql = "from " + entityName + " where id =:id";
      Query query = getSession().createQuery(hql);
      query.setParameter("id", id);
      List<?> rs = query.list();
      if (rs.isEmpty()) {
        return null;
      } else {
        return (T) rs.get(0);
      }
View Full Code Here

  }

  @SuppressWarnings("unchecked")
  public <T> List<T> getAll(Class<T> clazz) {
    String hql = "from " + Model.getEntityType(clazz).getEntityName();
    Query query = getSession().createQuery(hql);
    query.setCacheable(true);
    return query.list();
  }
View Full Code Here

    }
  }

  @SuppressWarnings("unchecked")
  public <T> List<T> searchNamedQuery(final String queryName, final Map<String, Object> params) {
    Query query = this.getSession().getNamedQuery(queryName);
    return QuerySupport.setParameter(query, params).list();
  }
View Full Code Here

  }

  @SuppressWarnings("unchecked")
  public <T> List<T> searchNamedQuery(final String queryName, final Map<String, Object> params,
      boolean cacheable) {
    Query query = getSession().getNamedQuery(queryName);
    query.setCacheable(cacheable);
    return QuerySupport.setParameter(query, params).list();
  }
View Full Code Here

    return QuerySupport.setParameter(query, params).list();
  }

  @SuppressWarnings("unchecked")
  public <T> List<T> searchNamedQuery(String queryName, Object... params) {
    Query query = getSession().getNamedQuery(queryName);
    return QuerySupport.setParameter(query, params).list();
  }
View Full Code Here

TOP

Related Classes of org.hibernate.Query

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.