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.abssh.util.GenericDao.java

License:Apache License

/**
 * ?Query,./* w ww.j  a  v a 2 s.  co  m*/
 */
protected Query setPageParameter(final Query q, final Page<T> page) {
    // hibernatefirstResult??0
    if (page.getPageSize() > 0) {
        q.setFirstResult(page.getFirst() - 1);
        q.setMaxResults(page.getPageSize());
    }
    return q;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ?Query,.//from   w  w w .j  a  v  a 2  s .  c om
 */
protected Query setPageParameterMap(final Query q, final Page<?> page) {
    // hibernatefirstResult??0
    if (page.getPageSize() > 0) {
        q.setFirstResult(page.getFirst() - 1);
        q.setMaxResults(page.getPageSize());
    }
    return q;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ?sql//from   w w  w. j av a 2s  .c  o m
 */
@SuppressWarnings("unchecked")
public T findSQLNonHibernateEntity(final String sql, final Map<String, Object> values) {
    Query query = createSQLQuery(sql, values).setResultTransformer(Transformers.aliasToBean(entityClass));
    query.setMaxResults(1);
    return (T) query.uniqueResult();
}

From source file:com.adibrata.smartdealer.dao.purchase.PurchaseEntryDao.java

@Override
public List<PurchaseOrderHdr> Paging(final int CurrentPage, final String WhereCond, final String SortBy)
        throws Exception {

    // TODO Auto-generated method stub
    final StringBuilder hql = new StringBuilder();
    List<PurchaseOrderHdr> list = null;
    try {/*w  w  w .j a  v a2 s.c o m*/
        hql.append(this.strStatement);
        if (!WhereCond.equals("")) {
            hql.append(WhereCond);
        }

        final Query selectQuery = this.session.createQuery(hql.toString());
        selectQuery.setFirstResult((CurrentPage - 1) * this.pagesize);
        selectQuery.setMaxResults(this.pagesize);
        list = selectQuery.list();

    } catch (final Exception exp) {

        final ExceptionEntities lEntExp = new ExceptionEntities();
        lEntExp.setJavaClass(Thread.currentThread().getStackTrace()[1].getClassName());
        lEntExp.setMethodName(Thread.currentThread().getStackTrace()[1].getMethodName());
        ExceptionHelper.WriteException(lEntExp, exp);
    }
    return list;
}

From source file:com.adibrata.smartdealer.dao.purchase.PurchaseEntryDao.java

@Override
public List<PurchaseOrderHdr> Paging(final int CurrentPage, final String WhereCond, final String SortBy,
        final boolean islast) throws Exception {
    // TODO Auto-generated method stub
    final StringBuilder hql = new StringBuilder();
    List<PurchaseOrderHdr> list = null;

    try {//from w  w  w.j  ava2s  .c  o  m
        hql.append(this.strStatement);
        if (!WhereCond.equals("")) {
            hql.append(" where ");
            hql.append(WhereCond);
        }
        final Query selectQuery = this.session.createQuery(hql.toString());
        final Long totalrecord = this.TotalRecord(this.strStatement, WhereCond);
        selectQuery.setFirstResult((int) ((totalrecord - 1) * this.pagesize));
        selectQuery.setMaxResults(this.pagesize);
        list = selectQuery.list();

    } catch (final Exception exp) {

        final ExceptionEntities lEntExp = new ExceptionEntities();
        lEntExp.setJavaClass(Thread.currentThread().getStackTrace()[1].getClassName());
        lEntExp.setMethodName(Thread.currentThread().getStackTrace()[1].getMethodName());
        ExceptionHelper.WriteException(lEntExp, exp);
    }
    return list;
}

From source file:com.algoTrader.entity.marketData.BarDaoBase.java

/**
 * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
 * @param queryObject//from  w w  w .j  av  a2 s.c  o m
 * @param transform
 * @param pageNumber
 * @param pageSize
 * @return PaginationResult
 */
@SuppressWarnings({ "unchecked" })
protected PaginationResult getPaginationResult(final Query queryObject, final int transform, int pageNumber,
        int pageSize) {
    try {
        final ScrollableResults scrollableResults = queryObject.scroll();
        scrollableResults.last();
        int totalCount = scrollableResults.getRowNumber();
        totalCount = totalCount >= 0 ? totalCount + 1 : 0;
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        // Unchecked transformation because Set object is reused, cannot be strongly typed.
        Set results = new LinkedHashSet(queryObject.list());
        transformEntities(transform, results);
        return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.PositionDaoBase.java

/**
 * {@inheritDoc}/*from   w ww . ja  v  a  2 s . com*/
 */
@Override
@SuppressWarnings("unchecked")
public List<?> findOpenPositions(final int transform, final String queryString, int pageNumber, int pageSize) {
    try {
        Query queryObject = super.getSession(false).createQuery(queryString);
        queryObject.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        List results = queryObject.list();
        transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.PositionDaoBase.java

/**
 * {@inheritDoc}/*from   ww w .j a  v  a 2  s  .  c  om*/
 */
@Override
@SuppressWarnings("unchecked")
public List<?> findOpenPositionsByStrategy(final int transform, final String queryString, int pageNumber,
        int pageSize, final String strategyName) {
    try {
        Query queryObject = super.getSession(false).createQuery(queryString);
        queryObject.setCacheable(true);
        queryObject.setParameter("strategyName", strategyName);
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        List results = queryObject.list();
        transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.security.SecurityDaoBase.java

/**
 * {@inheritDoc}//  ww w.  j  ava2  s .c  om
 */
@Override
@SuppressWarnings("unchecked")
public List<?> findSecuritiesInPortfolio(final int transform, final String queryString, int pageNumber,
        int pageSize) {
    try {
        Query queryObject = super.getSession(false).createQuery(queryString);
        queryObject.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        List results = queryObject.list();
        transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.security.SecurityDaoBase.java

/**
 * {@inheritDoc}//from  ww  w  .j  a  va  2s. c o  m
 */
@Override
@SuppressWarnings("unchecked")
public List<?> findSecuritiesOnWatchlist(final int transform, final String queryString, int pageNumber,
        int pageSize) {
    try {
        Query queryObject = super.getSession(false).createQuery(queryString);
        queryObject.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        List results = queryObject.list();
        transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}