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

@Override
public List<User> getUserList(Integer start, Integer max) {
    List<User> finalList = null;
    try {//  www .ja  va  2s . com
        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   w w w. java  2s .  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);
    }/* w  w  w.ja  v  a2s  .  c o  m*/
    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//w  ww  .  j  a v  a 2s.  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);
    }//from   w  w w.  j  a v  a2  s.co m

    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.ContentEntityDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<ContentKey> findBySpecification(ContentSpecification specification, String orderBy, int count) {

    Query compiled = getHibernateTemplate().getSessionFactory().getCurrentSession()
            .createQuery(getContentKeysHQL(specification, orderBy, false));

    compiled.setCacheable(true);/*from w ww  .j a v a 2 s. c  o m*/
    compiled.setMaxResults(count);

    @SuppressWarnings({ "unchecked" })
    List<ContentKey> list = compiled.list();

    List<ContentKey> contentKeys = new ArrayList<ContentKey>();
    for (ContentKey row : list) {
        contentKeys.add(row);
    }

    return contentKeys;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public List<ContentVersionKey> findBySpecification(ContentVersionSpecification specification, String orderBy,
        int count) {
    Query compiled = getHibernateTemplate().getSessionFactory().getCurrentSession()
            .createQuery(getContentVersionKeysHQL(specification, orderBy, false));
    compiled.setCacheable(true);//from ww w. j  a  v a2  s  .  c  o m
    compiled.setMaxResults(count);

    @SuppressWarnings({ "unchecked" })
    List<ContentVersionKey> list = compiled.list();

    List<ContentVersionKey> versionKeys = new ArrayList<ContentVersionKey>();
    for (ContentVersionKey row : list) {
        versionKeys.add(row);
    }

    return versionKeys;
}

From source file:com.ensa.model.controller.impl.ImageDAOImpl.java

@Override
public List<Image> getAllImages() {
    String hql = "FROM Image";
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hql);
    query.setMaxResults(20);
    List results = query.list();/*  w w w  . ja  v  a2 s  .c o  m*/

    if (results.isEmpty()) {
        return null;
    } else {
        return results;
    }

}

From source file:com.ensa.model.controller.impl.ImageDAOImpl.java

@Override
public List<Image> getLikedImages() {
    List<Image> liste = new ArrayList<Image>();
    Session session = this.sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Image img ORDER BY img.likes asc");
    query.setMaxResults(100);
    liste = query.list();//from  www. j  av  a  2 s  .  com

    return liste;
}

From source file:com.ensa.model.controller.impl.ImageDAOImpl.java

public List<Image> getDownloadedImages() {
    List<Image> liste = new ArrayList<Image>();
    Session session = this.sessionFactory.getCurrentSession();
    Query query = session.createQuery("FROM Image img ORDER BY img.downloads asc");
    query.setMaxResults(100);
    liste = query.list();//from   w ww.j  ava2s.c  o  m
    return liste;
}