Example usage for org.hibernate Query setMaxResults

List of usage examples for org.hibernate Query setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Query setMaxResults.

Prototype

@Override
    Query<R> setMaxResults(int maxResult);

Source Link

Usage

From source file:com.gp.cvst.logisoft.hibernate.dao.FclDoorDeliveryDAO.java

public FclDoorDelivery getFclDoorDeliveryByBol(Integer bolId) throws Exception {
    String queryString = "from FclDoorDelivery where bolId=:bolId";
    Query queryObj = getSession().createQuery(queryString);
    queryObj.setParameter("bolId", bolId);
    return (FclDoorDelivery) queryObj.setMaxResults(1).uniqueResult();
}

From source file:com.green.common.persistence.BaseDao.java

License:Open Source License

/**
 * QL //from w  w w. ja va  2  s .co m
 * @param page
 * @param qlString
 * @param parameter
 * @return
 */
@SuppressWarnings("unchecked")
public <E> Page<E> find(Page<E> page, String qlString, Parameter parameter) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countQlString = "select count(*) " + removeSelect(removeOrders(qlString));
        //           page.setCount(Long.valueOf(createQuery(countQlString, parameter).uniqueResult().toString()));
        Query query = createQuery(countQlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String ql = qlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        ql += " order by " + page.getOrderBy();
    }
    Query query = createQuery(ql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    page.setList(query.list());
    return page;
}

From source file:com.green.common.persistence.BaseDao.java

License:Open Source License

/**
* SQL //from   ww  w.  jav  a2s  .  com
* @param page
* @param sqlString
* @param resultClass
* @param parameter
* @return
*/
@SuppressWarnings("unchecked")
public <E> Page<E> findBySql(Page<E> page, String sqlString, Parameter parameter, Class<?> resultClass) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countSqlString = "select count(*) " + removeSelect(removeOrders(sqlString));
        //           page.setCount(Long.valueOf(createSqlQuery(countSqlString, parameter).uniqueResult().toString()));
        Query query = createSqlQuery(countSqlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String sql = sqlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        sql += " order by " + page.getOrderBy();
    }
    SQLQuery query = createSqlQuery(sql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    setResultTransformer(query, resultClass);
    page.setList(query.list());
    return page;
}

From source file:com.hasz.dao.EstadoDAO.java

public static Estado buscarEstadoById(int idEstado) {
    Session sessao = HibernateUtil.getSession();
    Transaction t = sessao.beginTransaction();
    Estado retorno = new Estado();
    try {//from   www . j a  v  a2  s .com
        Query select = sessao.createQuery("from Estado where idEstado = :idEstado");
        select.setInteger("idEstado", idEstado);
        select.setFirstResult(0);//indica o primeiro registro a ser retornado (0  o primeiro)
        select.setMaxResults(1);//sem esse campo, busca todos os registros
        List<Estado> estados = select.list();

        if (estados.size() == 1) {
            retorno = estados.get(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
        retorno = null;
    } finally {
        t.commit();
        sessao.close();
    }
    return retorno;
}

From source file:com.heimaide.server.common.persistence.BaseDao.java

License:Open Source License

/**
 * QL //from w  ww  . j  a va 2s .  c om
 *
 * @param page
 * @param qlString
 * @param parameter
 * @return
 */
@SuppressWarnings("unchecked")
public <E> Page<E> find(Page<E> page, String qlString, Parameter parameter) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countQlString = "select count(*) " + removeSelect(removeOrders(qlString));
        // page.setCount(Long.valueOf(createQuery(countQlString,
        // parameter).uniqueResult().toString()));
        Query query = createQuery(countQlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String ql = qlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        ql += " order by " + page.getOrderBy();
    }
    Query query = createQuery(ql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    page.setList(query.list());
    return page;
}

From source file:com.heimaide.server.common.persistence.BaseDao.java

License:Open Source License

/**
 * 2015720/*from w  w  w .  j  a  v  a2s  .  c o m*/
 *
 * @param qlString
 * @param parameter
 * @return
 */
@SuppressWarnings("unchecked")
public <E> Page<E> findPage(Page<E> page, String qlString, List param) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countQlString = "select count(*) " + removeSelect(removeOrders(qlString));
        // page.setCount(Long.valueOf(createQuery(countQlString,
        // parameter).uniqueResult().toString()));
        Query query = createQuery(countQlString, param);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String ql = qlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        ql += " order by " + page.getOrderBy();
    }
    Query query = createQuery(ql, param);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    page.setList(query.list());
    return page;
}

From source file:com.heimaide.server.common.persistence.BaseDao.java

License:Open Source License

public <E> Page<E> findBySql(Page<E> page, String sqlString, Parameter parameter, Class<?> resultClass,
        ResultTransformer rtf) {//  w w w .j a v  a  2  s. c o  m
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countSqlString = "select count(*) " + removeSelect(removeOrders(sqlString));
        // page.setCount(Long.valueOf(createSqlQuery(countSqlString,
        // parameter).uniqueResult().toString()));
        Query query = createSqlQuery(countSqlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String sql = sqlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        sql += " order by " + page.getOrderBy();
    }
    SQLQuery query = createSqlQuery(sql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    if (rtf != null) {
        query.setResultTransformer(rtf);
    } else {
        setResultTransformer(query, resultClass);
    }
    page.setList(query.list());
    return page;
}

From source file:com.heimaide.server.common.persistence.BaseDao.java

License:Open Source License

public <E> Page<E> queryBySqlFromDB(Page<E> page, String sqlString, Class<?> resultClass, List param) {
    // get count//from   ww  w. j a  v  a2s . co  m
    if (!page.isDisabled() && !page.isNotCount()) {
        String countSqlString = "select count(*) from ( " + sqlString + ") k";// ?????
        // page.setCount(Long.valueOf(createSqlQuery(countSqlString,
        // parameter).uniqueResult().toString()));
        Query query = createSqlQuery(countSqlString, param);
        query.setCacheMode(CacheMode.REFRESH);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String sql = sqlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        sql += " order by " + page.getOrderBy();
    }
    SQLQuery query = createSqlQuery(sql, param);
    query.setCacheMode(CacheMode.REFRESH);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    /*
     * Field[] fields = resultClass.getFields(); for (Field f : fields) {
    * query.addScalar(f.getName());// }
    */
    query.setResultTransformer(Transformers.aliasToBean(resultClass));
    page.setList(query.list());
    return page;
}

From source file:com.heimaide.server.common.persistence.BaseDao.java

License:Open Source License

public <E> Page<E> queryBySql(Page<E> page, String sqlString, Class<?> resultClass, List param) {
    // get count/*from www.  ja  v  a2 s  .c o m*/
    if (!page.isDisabled() && !page.isNotCount()) {
        String countSqlString = "select count(*) from ( " + sqlString + ") k";// ?????
        // page.setCount(Long.valueOf(createSqlQuery(countSqlString,
        // parameter).uniqueResult().toString()));
        Query query = createSqlQuery(countSqlString, param);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String sql = sqlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        sql += " order by " + page.getOrderBy();
    }
    SQLQuery query = createSqlQuery(sql, param);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    /*
    * Field[] fields = resultClass.getFields(); for (Field f : fields) {
    * query.addScalar(f.getName());// }
    */
    query.setResultTransformer(Transformers.aliasToBean(resultClass));
    page.setList(query.list());
    return page;
}

From source file:com.hmsinc.epicenter.model.analysis.impl.MaterializedViewAnalysisQueries.java

License:Open Source License

@SuppressWarnings("unchecked")
public org.hibernate.Criteria createGetCasesQuery(AnalysisParameters analysisParameters, Long offset,
        Long numRows) {/*  w  w  w.  j a  va2  s.  c  o  m*/

    Validate.notNull(offset, "Offset must be set.");
    Validate.notNull(numRows, "Number of returned rows must be set.");
    Validate.notNull(analysisParameters.getDataType(), "Data type must be set.");

    Class<? extends Interaction> interactionClass = null;
    for (ClassificationTarget target : analysisParameters.getDataType().getTargets()) {
        Validate.notNull(target.getInteractionClass(), "Interaction class was null for " + target.toString());
        if (interactionClass == null) {
            interactionClass = target.getInteractionClass();
        } else {
            Validate.isTrue(interactionClass.equals(target.getInteractionClass()),
                    "Inconsistent interaction classes");
        }
    }

    // Query is a subselect of Interaction based on materialized view.
    final Class<? extends Serializable> mv = (analysisParameters.getClassifications() == null
            || analysisParameters.getClassifications().size() == 0 ? AnalysisView.class
                    : ClassifiedAnalysisView.class);

    final QueryBuilder query = new QueryBuilder().addEntity(mv, "analysis").addProperty("distinct(analysis.id)")
            .addProperty("analysis.interactionDate").addOrder("analysis.interactionDate asc");

    applyAttributes(query, analysisParameters);
    applyClassifications(query, analysisParameters);

    // Geography
    applyGeography(query, analysisParameters.getContainer().getClass(), analysisParameters);

    disableNestedLoops(entityManager);

    final Query q = query.toQuery((Session) entityManager.getDelegate(), true);

    q.setFirstResult(offset.intValue());
    q.setMaxResults(numRows.intValue());
    final List<Object[]> items = q.list();
    final List<Long> interactionList = new ArrayList<Long>();
    for (Object[] item : items) {
        interactionList.add((Long) item[0]);
    }
    Criteria ret = null;

    enableNestedLoops(entityManager);

    if (interactionList.size() > 0) {

        // We need to use a ResultTransformer here to avoid duplicate rows.
        ret = criteriaQuery(entityManager, interactionClass).setFetchMode("classifications", FetchMode.JOIN)
                .setFetchMode("patient", FetchMode.JOIN).setFetchMode("patientDetail", FetchMode.JOIN)
                .add(Restrictions.in("id", interactionList))
                .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    }
    return ret;
}