Example usage for org.hibernate Query setFirstResult

List of usage examples for org.hibernate Query setFirstResult

Introduction

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

Prototype

@Override
    Query<R> setFirstResult(int startPosition);

Source Link

Usage

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

License:Open Source License

/**
 * QL /*from w  w  w .  j  av a 2  s .  c  o 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. j  a v a2  s. 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  w  w  w  .j  a  va 2  s  .c  o  m
        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 w  w.j av  a  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.heimaide.server.common.persistence.BaseDao.java

License:Open Source License

/**
 * 2015720/*ww w  .  j  a v a 2 s  .co 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) {/*from ww  w.j a v a2 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//ww  w. j a  va2s. com
    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 w ww  .  j  a  va  2  s . c  om
    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) {/*from   w w  w  .j a  v  a  2 s  . c om*/

    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;
}

From source file:com.huateng.commquery.dao.CommQueryDAO.java

License:Open Source License

public List<TxnModel> findBySQLQueryModel(final String sql, final int begin, final int count) {

    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createSQLQuery(sql).addScalar("TERM_ID", Hibernate.STRING)
                    .addScalar("INST_DATE", Hibernate.STRING).addScalar("TXN_TYPE", Hibernate.STRING)
                    .addScalar("CUP_SSN", Hibernate.STRING).addScalar("REFE_NUM", Hibernate.STRING)
                    .addScalar("ORDER_TRSNUM", Hibernate.STRING).addScalar("TXN_STATE", Hibernate.STRING)
                    .addScalar("PAN", Hibernate.STRING).addScalar("TRS_AMOUNT", Hibernate.STRING)
                    .addScalar("IPS_MERCHT", Hibernate.STRING)

                    .setResultTransformer(Transformers.aliasToBean(TxnModel.class));
            if (begin >= 0) {
                query.setFirstResult(begin);
                query.setMaxResults(count);
            }/*from  www .j  av  a  2  s. com*/
            List<TxnModel> list = query.list();

            return list;
        }
    });
}