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.krawler.dao.BaseDAO.java

License:Open Source License

/**
 * Executes an update query using the provided hql, query parameters and
 * paging parameters/*from   w w  w .j  a va 2 s.c o  m*/
 * 
 * @param hql
 *            Query to execute
 * @param params
 *            Query parameters
 * @param pagingParam
 *            paging parameters
 * @return List
 */
public int executeUpdatePaging(final String hql, final Object[] params, final Integer[] pagingParam) {
    int numRow = 0;
    numRow = (Integer) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) {
            int numRows = 0;
            Query query = session.createQuery(hql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    query.setParameter(i, params[i]);
                }
            }
            query.setFirstResult(pagingParam[0]);
            query.setMaxResults(pagingParam[1]);
            numRows = query.executeUpdate();
            session.flush();
            session.clear();
            return numRows;
        }
    });

    return numRow;
}

From source file:com.krawler.esp.hibernate.impl.HibernateUtil.java

License:Open Source License

public static List executeQueryPaging(Session session, String hql, Object[] params, Integer[] pagingParam)
        throws ServiceException {
    List results = null;/*w  w  w  .  java 2  s  . c  om*/
    try {
        Query query = session.createQuery(hql);

        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                query.setParameter(i, params[i]);
            }
        }
        query.setFirstResult(pagingParam[0]);
        query.setMaxResults(pagingParam[1]);

        results = query.list();
    } catch (HibernateException e) {
        String message = "SQL: '" + hql + "'";
        throw ServiceException.FAILURE(message, e);
    } finally {
        //         DbPool.closeStatement(stmt);
    }
    return results;
}

From source file:com.krawler.esp.hibernate.impl.HibernateUtil.java

License:Open Source License

public static List executeQueryPaging(HibernateTemplate hibernateTemplate, final String hql,
        final Object[] params, final Integer[] pagingParam) throws ServiceException {
    List results = null;//w  ww .  j a  va  2  s.c o  m
    try {
        results = hibernateTemplate.executeFind(new HibernateCallback() {

            public Object doInHibernate(Session session) {
                Query query = session.createQuery(hql);
                if (params != null) {
                    for (int i = 0; i < params.length; i++) {
                        query.setParameter(i, params[i]);
                    }
                }
                query.setFirstResult(pagingParam[0]);
                query.setMaxResults(pagingParam[1]);
                return query.list();
            }
        });
    } catch (HibernateException e) {
        String message = "SQL: '" + hql + "'";
        throw ServiceException.FAILURE(message, e);
    }
    return results;
}

From source file:com.krawler.hql.payroll.payrollManager.java

License:Open Source License

public String getAuditData(String stdate, String enddate, String Cid, String start, String limit,
        Session session) {/* w ww .j a  v a  2s  .  co m*/
    try {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        Date d2 = new Date(enddate);
        Date d3 = new Date(stdate);
        String ssttrr = df.format(d3);
        Query query = session
                .createQuery("from AudittrailPayroll where companyid=:cid and date between :st and :end");
        query.setString("st", df.format(d3));
        query.setString("end", df.format(d2));
        query.setString("cid", Cid);
        query.setFirstResult(Integer.parseInt(start));
        query.setMaxResults(Integer.parseInt(limit));
        List lst = (List) query.list();
        JSONObject jobj = new JSONObject();
        int i = 0;
        int x = 0;
        for (i = 0; i < lst.size(); i++) {
            AudittrailPayroll at = (AudittrailPayroll) lst.get(i);
            JSONObject jobjtemp = new JSONObject();
            jobjtemp.put("companyid", at.getCompanyid());
            jobjtemp.put("date", at.getDate());
            jobjtemp.put("ip", at.getIp());
            jobjtemp.put("typeofwork", at.getTypeofwork());
            jobjtemp.put("username", at.getUserid());
            jobj.append("data", jobjtemp);
        }
        query = session
                .createQuery("from AudittrailPayroll where companyid=:cid and date between :st and :end");
        query.setString("st", df.format(d3));
        query.setString("end", df.format(d2));
        query.setString("cid", Cid);
        lst = (List) query.list();
        jobj.put("success", true);
        JSONObject jobj1 = new JSONObject();
        jobj1.put("valid", true);
        jobj1.put("totalcount", lst.size());
        jobj1.put("data", jobj);
        return (jobj1.toString());
    } catch (Exception e) {
        return (e.getMessage());
    }
}

From source file:com.kunckle.jetpower.core.base.repository.impl.BaseRepositorySupport.java

License:Apache License

/**
 * /*from   www  . j av a  2s. co  m*/
 *
 * @param pageAdapter ?
 * @return null
 * @throws IndexOutOfBoundsException
 */
@Override
@SuppressWarnings("unchecked")
public Page<E> findAll(PageAdapter pageAdapter) {
    Query query = getSession().createQuery(
            "from " + entityClass.getSimpleName() + " " + pageAdapter.getPageSortAdapter().getPrepQL());
    pageAdapter.setCountOfElements(count());
    if (pageAdapter.exists() && pageAdapter.getPageNumber() != 0) {
        throw new IndexOutOfBoundsException("?");
    } else if (pageAdapter.getCountOfElements() == 0) {
        return null;
    }
    query.setFirstResult(pageAdapter.getPageOffset() * pageAdapter.getPageSize());
    query.setMaxResults(pageAdapter.getPageSize());
    return new Page<E>(pageAdapter, query.list());
}

From source file:com.kunckle.jetpower.core.base.repository.impl.BaseRepositorySupport.java

License:Apache License

/**
 * ?//from w ww .  java  2s  . c om
 *
 * @param searchAdapter ??
 * @return ?null
 * @throws IndexOutOfBoundsException
 */
@Override
@SuppressWarnings("unchecked")
public Page<E> findAll(SearchAdapter searchAdapter, PageAdapter pageAdapter) {
    pageAdapter.setCountOfElements(count(searchAdapter));
    if (pageAdapter.exists() && pageAdapter.getPageNumber() != 0) {
        throw new IndexOutOfBoundsException("?");
    } else if (pageAdapter.getCountOfElements() == 0) {
        return null;
    }

    if (searchAdapter.getPrepQL() != null) {
        Query query = getSession().createQuery("from " + entityClass.getSimpleName() + " "
                + searchAdapter.getPrepQL() + " " + pageAdapter.getPageSortAdapter().getPrepQL());
        query.setParameter(0, entityClass.getName());
        query.setFirstResult(pageAdapter.getPageOffset() * pageAdapter.getPageSize());
        query.setMaxResults(pageAdapter.getPageSize());
        return new Page<E>(pageAdapter, query.list());
    } else {
        Criteria criteria = searchAdapter.getCriteria(getSession().createCriteria(entityClass));
        criteria = pageAdapter.getPageSortAdapter().getCriteria(criteria);
        criteria.setFirstResult(pageAdapter.getPageOffset() * pageAdapter.getPageSize());
        criteria.setMaxResults(pageAdapter.getPageSize());
        return new Page<E>(pageAdapter, criteria.list());
    }
}

From source file:com.lakeside.data.sqldb.PageBaseDao.java

License:Apache License

/**
 * ?Query,.//from w  ww  . j  av  a2s  .c  o m
 */
protected <X> Query setPageParameter(final Query q, final Page<X> page) {
    //hibernatefirstResult??0
    q.setFirstResult(page.getFirst() - 1);
    q.setMaxResults(page.getPageSize());
    return q;
}

From source file:com.levinas.ecole.dao.EnfantDaoImpl.java

@Override
public HashMap listAll(int page, int rpp, String search) {
    Integer iRpp = rpp;// ww  w  . j  a  va 2  s.c  om
    HashMap result = new HashMap();

    Session session = sessionFactory.getCurrentSession();
    Query query = session
            .createQuery("SELECT e FROM Enfant e WHERE e.nom like :search OR e.prenom like :search");
    query.setParameter("search", search);
    double nbResult = query.list().size();

    query.setFirstResult((page - 1) * rpp);
    query.setMaxResults(rpp);
    List listItems = query.list();

    result.put("items", listItems);
    result.put("page_count", Math.ceil(nbResult / iRpp.doubleValue()));
    result.put("total_items", nbResult);
    return result;
}

From source file:com.levinas.ecole.dao.PersonnelDaoImpl.java

@Override
public HashMap listAll(int page, int rpp, String search) {
    Integer iRpp = rpp;//  www . ja v a  2 s. com
    HashMap result = new HashMap();

    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(
            "SELECT p FROM Personnel p WHERE p.telMobile like :search OR p.fonction like :search OR p.telFixe like :search OR p.nom like :search OR p.prenom like :search OR p.adresse like :search OR p.ville like :search OR p.codepostal like :search");
    query.setParameter("search", search);
    double nbResult = query.list().size();

    query.setFirstResult((page - 1) * rpp);
    query.setMaxResults(rpp);
    List listItems = query.list();

    result.put("items", listItems);
    result.put("page_count", Math.ceil(nbResult / iRpp.doubleValue()));
    result.put("total_items", nbResult);

    return result;
}

From source file:com.levinas.ecole.dao.ResponsableDaoImpl.java

public HashMap listAll(int page, int rpp, String search, boolean all) {
    Integer iRpp = rpp;/*from   w  w  w  . j  a v  a2s.c  om*/
    HashMap result = new HashMap();

    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(
            "SELECT r FROM Responsable r WHERE r.nom like :search OR r.prenom like :search OR r.adresse like :search OR r.ville like :search OR r.codePostale like :search");
    query.setParameter("search", search);
    double nbResult = query.list().size();

    if (!all) {
        query.setFirstResult((page - 1) * rpp);
        query.setMaxResults(rpp);
    }
    List listItems = query.list();

    result.put("items", listItems);
    result.put("page_count", Math.ceil(nbResult / iRpp.doubleValue()));
    result.put("total_items", nbResult);

    return result;
}