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.egs.blog.backend.dao.PostDAOImpl.java

@Override
public List<Post> getPostList(Integer start, Integer max) {
    List<Post> finalList = null;
    try {/*from   w  ww .  j  a  v a2 s.co m*/
        Query query = getSession().createQuery("SELECT p FROM Post p WHERE p.id > 0 ORDER BY p.id DESC");
        if (start != null) {
            query.setFirstResult(start);
        }
        if (max != null) {
            query.setMaxResults(max);
        }
        finalList = query.list();

        if (finalList == null) {
            return null;
        }
        //   System.out.println("finalList " + finalList.size());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return finalList;
}

From source file:com.egs.blog.backend.dao.UserDAOImpl.java

@Override
public List<User> getUserList(Integer start, Integer max) {
    List<User> finalList = null;
    try {/* w w  w .  j  av  a 2 s.c  o m*/
        Query query = getSession().createQuery("SELECT c FROM User c WHERE c.id > 0 ORDER BY c.id DESC");
        if (start != null) {
            query.setFirstResult(start);
        }
        if (max != null) {
            query.setMaxResults(max);
        }
        finalList = query.list();

        if (finalList == null) {
            return null;
        }
        //   System.out.println("finalList " + finalList.size());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return finalList;
}

From source file:com.ejushang.steward.common.genericdao.dao.hibernate.GeneralDAO.java

License:Apache License

/**
 * hql//from   ww  w. java2 s.  c o  m
 * @param ql
 * @param page ?null
 * @param queryPrepare ?query?,?null
 * @param args
 * @param <RT>
 * @return
 */
public <RT> List<RT> queryWithPrepare(String ql, Page page, QueryPrepare queryPrepare, Object... args) {
    Query query = getSession().createQuery(ql);
    for (int i = 0; i < args.length; i++) {
        query.setParameter(i, args[i]);
    }
    if (queryPrepare != null) {
        queryPrepare.prepare(query);
    }
    if (page == null) {
        return query.list();
    }

    if (this.hasGroupBy(ql)) {
        //count
        StringBuilder countQL = new StringBuilder("select count(1) ");
        countQL.append(ql.substring(ql.indexOf("from"), ql.lastIndexOf("order")));
        countQL = new StringBuilder(removeFetchInCountQl(countQL.toString()));
        Query countQuery = getSession().createQuery(countQL.toString());
        for (int i = 0; i < args.length; i++) {
            countQuery.setParameter(i, args[i]);
        }
        List count = countQuery.list();
        page.setTotalCount(count.size());

    } else {
        //count
        page.setTotalCount(this.count(query.getQueryString(), args));
    }

    query.setFirstResult(page.getStart());
    query.setMaxResults(page.getLimit());
    page.setResult(query.list());
    return page.getResult();
}

From source file:com.ejushang.steward.common.genericdao.search.hibernate.HibernateSearchProcessor.java

License:Apache License

private void addPaging(Query query, ISearch search) {
    int firstResult = SearchUtil.calcFirstResult(search);
    if (firstResult > 0) {
        query.setFirstResult(firstResult);
    }//from   ww  w  . ja v  a  2 s .c  om
    if (search.getMaxResults() > 0) {
        query.setMaxResults(search.getMaxResults());
    }
}

From source file:com.ejushang.uams.server.common.genericdao.dao.hibernate.GeneralDAO.java

License:Apache License

/**
 * hql/*from  w w  w  .  j a v  a 2  s  .c om*/
 * @param ql
 * @param page ?null
 * @param args
 * @param <RT>
 * @return
 */
public <RT> List<RT> query(String ql, Page page, Object... args) {
    Query query = getSession().createQuery(ql);
    for (int i = 0; i < args.length; i++) {
        query.setParameter(i, args[i]);
    }
    if (page == null) {
        return query.list();
    }

    if (this.hasGroupBy(ql)) {
        //count
        StringBuilder countQL = new StringBuilder("select count(*) ");
        countQL.append(ql.substring(ql.indexOf("from"), ql.lastIndexOf("order")));
        Query countQuery = getSession().createQuery(countQL.toString());
        for (int i = 0; i < args.length; i++) {
            countQuery.setParameter(i, args[i]);
        }
        List count = countQuery.list();
        page.setTotalCount(count.size());

    } else {
        //count
        page.setTotalCount(this.count(query.getQueryString(), args));
    }

    query.setFirstResult(page.getStart());
    query.setMaxResults(page.getLimit());
    page.setResult(query.list());
    return page.getResult();
}

From source file:com.enonic.cms.store.dao.AbstractBaseEntityDao.java

License:Open Source License

@SuppressWarnings("unchecked")
private List<T> findPageItems(Class<T> clz, String filter, int index, int count) {
    final StringBuffer hql = new StringBuffer("select x from ");
    hql.append(clz.getName()).append(" x");

    if (filter != null) {
        hql.append(" where ").append(filter);
    }//  w  w w .  ja  v  a 2s.  com

    final Query query = createQuery(hql.toString());
    query.setFirstResult(index);
    query.setMaxResults(count);
    return (List<T>) query.list();
}

From source file:com.enonic.cms.store.dao.UserEntityDao.java

License:Open Source License

public List<UserEntity> findByUserStoreKey(final UserStoreKey userStoreKey, final Integer index,
        final Integer count, final boolean includeDeleted) {

    return executeListResult(UserEntity.class, new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.getNamedQuery("UserEntity.findByUserStoreKey");
            if (index != null) {
                query.setFirstResult(index);
            }/* w  ww  .  ja va2s. c  o m*/
            query.setInteger("userStoreKey", userStoreKey.toInt());
            query.setInteger("deleted", includeDeleted ? 1 : 0);

            List list = query.list();
            if (count == null) {
                return list;
            } else {
                return list.subList(0, Math.min(count, list.size()));
            }
        }
    });
}

From source file:com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery.java

License:Apache License

public Query getAsHqlQuery(Session session) {
    String text = getAsHqlText(0);
    LOGGER.trace("HQL text generated:\n{}", text);
    Query query = session.createQuery(text);
    for (Map.Entry<String, QueryParameterValue> parameter : parameters.entrySet()) {
        String name = parameter.getKey();
        QueryParameterValue parameterValue = parameter.getValue();
        LOGGER.trace("Parameter {} = {}", name, parameterValue.debugDump());

        if (parameterValue.getValue() instanceof Collection) {
            if (parameterValue.getType() != null) {
                query.setParameterList(name, (Collection) parameterValue.getValue(), parameterValue.getType());
            } else {
                query.setParameterList(name, (Collection) parameterValue.getValue());
            }/* w w w .  j  av a  2 s. c  om*/
        } else {
            if (parameterValue.getType() != null) {
                query.setParameter(name, parameterValue.getValue(), parameterValue.getType());
            } else {
                query.setParameter(name, parameterValue.getValue());
            }
        }
    }
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }
    if (firstResult != null) {
        query.setFirstResult(firstResult);
    }
    if (resultTransformer != null) {
        query.setResultTransformer(resultTransformer);
    }
    return query;
}

From source file:com.fiveamsolutions.nci.commons.service.AbstractBaseAggregateSearchBean.java

License:Open Source License

/**
 * {@inheritDoc}//from w ww  .  j av a  2 s .  c  o m
 * 
 */
public Map<S, Long> aggregate(GroupableSearchCriteria<S, T> criteria, PageSortParams<T> pageSortParams,
        List<? extends GroupByCriteria<T>> groupByCriterias) {
    validateSearchCriteria(criteria);
    StringBuffer orderBy = new StringBuffer("");
    StringBuffer joinClause = new StringBuffer("");
    if (pageSortParams != null) {
        processFixedSortCriteria(criteria, pageSortParams, orderBy, joinClause);
        processDynamicSortCriteria(criteria, pageSortParams, orderBy);
    }

    StringBuffer groupBy = new StringBuffer("");
    if (groupByCriterias != null) {
        processFixedGroupByCriteria(criteria, groupByCriterias, groupBy);
    }

    Query q;
    q = criteria.getQuery(orderBy.toString(), joinClause.toString(), groupBy.toString(), false);

    if (pageSortParams != null) {
        q.setMaxResults(pageSortParams.getPageSize());
        if (pageSortParams.getIndex() > 0) {
            q.setFirstResult(pageSortParams.getIndex());
        }
    }

    return getAggregateResults(criteria, q);
}

From source file:com.fiveamsolutions.nci.commons.service.AbstractBaseSearchBean.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww  w.  jav a  2 s. co m*/
 */
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public List<T> search(SearchCriteria<T> criteria, PageSortParams<T> pageSortParams) {
    validateSearchCriteria(criteria);
    StringBuffer orderBy = new StringBuffer("");
    StringBuffer joinClause = new StringBuffer("");
    if (pageSortParams != null) {
        processFixedSortCriteria(criteria, pageSortParams, orderBy, joinClause);
        processDynamicSortCriteria(criteria, pageSortParams, orderBy);
    }

    Query q = criteria.getQuery(orderBy.toString(), joinClause.toString(), false);

    if (pageSortParams != null) {
        q.setMaxResults(pageSortParams.getPageSize());
        if (pageSortParams.getIndex() > 0) {
            q.setFirstResult(pageSortParams.getIndex());
        }
    }

    return getResultList(q);
}