Example usage for org.hibernate Criteria setMaxResults

List of usage examples for org.hibernate Criteria setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Criteria setMaxResults.

Prototype

public Criteria setMaxResults(int maxResults);

Source Link

Document

Set a limit upon the number of objects to be retrieved.

Usage

From source file:GetGroupChatServ.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w  ww  .ja  va  2s . c om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {

        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

        int gid = Integer.parseInt(request.getParameter("gid"));

        GroupDetail gd = (GroupDetail) ss.get(GroupDetail.class, gid);

        System.out.println("Group id is :" + gd.getGId());

        Criteria cr = ss.createCriteria(ChatMsg.class);
        cr.add(Restrictions.eq("gId", gd));
        cr.addOrder(Order.desc("cmid"));
        cr.setMaxResults(25);
        ArrayList<ChatMsg> cm = (ArrayList<ChatMsg>) cr.list();
        Collections.reverse(cm);

        System.out.println("size of the msg array :" + cm.size());
        request.setAttribute("msgs", cm);
        request.setAttribute("gobj", gd);

        RequestDispatcher rd = request.getRequestDispatcher("groupchat.jsp");
        rd.forward(request, response);

    } catch (HibernateException he) {
        System.out.print(he.getMessage());
    }
}

From source file:abid.password.swing.dao.hibernate.AbstractHibernateDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<T> get(int first, int fetchSize) {
    Session session = sessionFactory.openSession();
    try {// w w  w . j a v a 2  s.c  o  m
        Criteria criteria = session.createCriteria(persistentClass);
        criteria.setFirstResult(first);
        criteria.setMaxResults(fetchSize);
        criteria.setFetchSize(fetchSize);
        criteria.setCacheable(true);

        List<T> dataList = (List<T>) criteria.list();
        return dataList;
    } finally {
        session.close();
    }
}

From source file:abid.password.swing.dao.hibernate.AbstractHibernateDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<T> getSorted(int first, int fetchSize, String propertyName, boolean ascending) {
    Session session = sessionFactory.openSession();
    try {//  ww w  . j a v  a2  s  .c  o  m
        Criteria criteria = session.createCriteria(persistentClass);
        criteria.setFirstResult(first);
        criteria.setMaxResults(fetchSize);
        criteria.setFetchSize(fetchSize);
        criteria.setCacheable(true);

        if (ascending) {
            criteria.addOrder(Order.asc(propertyName));
        } else {
            criteria.addOrder(Order.desc(propertyName));
        }

        List<T> dataList = (List<T>) criteria.list();
        return dataList;
    } finally {
        session.close();
    }
}

From source file:abid.password.wicket.dao.AbstractHibernateDao.java

License:Apache License

@Transactional(type = TransactionType.READ_ONLY)
@SuppressWarnings("unchecked")
public List<T> get(int first, int fetchSize) {
    Criteria criteria = session.get().createCriteria(persistentClass);
    criteria.setFirstResult(first);/*  w  w w  . j  av  a  2  s. co  m*/
    criteria.setMaxResults(fetchSize);
    criteria.setFetchSize(fetchSize);
    criteria.setCacheable(true);

    List<T> dataList = (List<T>) criteria.list();
    return dataList;
}

From source file:abid.password.wicket.dao.AbstractHibernateDao.java

License:Apache License

@Transactional(type = TransactionType.READ_ONLY)
@SuppressWarnings("unchecked")
public List<T> getSorted(int first, int fetchSize, String propertyName, boolean ascending) {
    Criteria criteria = session.get().createCriteria(persistentClass);
    criteria.setFirstResult(first);/*  w w  w . ja va 2  s  .com*/
    criteria.setMaxResults(fetchSize);
    criteria.setFetchSize(fetchSize);
    criteria.setCacheable(true);

    if (ascending) {
        criteria.addOrder(Order.asc(propertyName));
    } else {
        criteria.addOrder(Order.desc(propertyName));
    }

    List<T> dataList = (List<T>) criteria.list();
    return dataList;
}

From source file:Action.StockManagement.java

public String viewStock() throws NullPointerException {
    Session s = HibernateUtil.getSession();
    Transaction t = s.beginTransaction();

    Criteria c = s.createCriteria(Current_Stock.class);
    c.setFirstResult(0);/*from   w  w  w .j a v a  2  s .  c  om*/
    c.setMaxResults(10);
    List<Current_Stock> list = c.list();
    getReq().setAttribute("cs", list);

    /* Query q=s.createQuery(" from Current_Stock"); 
    List<Current_Stock> data=q.list();
    req.setAttribute("cs",data);
    int i=0;
    req.setAttribute("next",i);*/

    t.commit();
    return "success";
}

From source file:Action.StockManagement.java

public String viewNext() throws NullPointerException {
    Session s = HibernateUtil.getSession();
    Transaction t = s.beginTransaction();

    // int i=Integer.parseInt(req.getParameter("first"));
    Criteria c = s.createCriteria(Current_Stock.class);
    c.setFirstResult(10);/*from ww  w. j ava2  s .c o m*/
    c.setMaxResults(10);
    List<Current_Stock> list = c.list();
    getReq().setAttribute("cs", list);

    // req.setAttribute("first",i+2);
    /* Query q=s.createQuery(" from Current_Stock"); 
    List<Current_Stock> data=q.list();
    req.setAttribute("cs",data);
    int i=0;
    req.setAttribute("next",i);*/

    t.commit();
    return "success";
}

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * /*  w  w w .ja v a2 s .  c o  m*/
 * @param page
 * @param detachedCriteria
 * @param resultTransformer
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> find(Page<T> page, DetachedCriteria detachedCriteria, ResultTransformer resultTransformer) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        page.setCount(count(detachedCriteria));
        if (page.getCount() < 1) {
            return page;
        }
    }
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(resultTransformer);
    // set page
    if (!page.isDisabled()) {
        criteria.setFirstResult(page.getFirstResult());
        criteria.setMaxResults(page.getMaxResults());
    }
    // order by
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        for (String order : StringUtils.split(page.getOrderBy(), ",")) {
            String[] o = StringUtils.split(order, " ");
            if (o.length == 1) {
                criteria.addOrder(Order.asc(o[0]));
            } else if (o.length == 2) {
                if ("DESC".equals(o[1].toUpperCase())) {
                    criteria.addOrder(Order.desc(o[0]));
                } else {
                    criteria.addOrder(Order.asc(o[0]));
                }
            }
        }
    }
    page.setList(criteria.list());
    return page;
}

From source file:ar.com.zauber.commons.repository.SpringHibernateRepository.java

License:Apache License

/** @see Repository#find(Query) */
@SuppressWarnings("unchecked")
public final <T extends Persistible> List<T> find(final Query<T> query) {
    CriteriaSpecification criteria = getCriteriaSpecification(null, query, false);
    final SimpleQuery<T> simpleQuery = (SimpleQuery<T>) query;
    Criteria aCriteria;/*  w  w  w . j av  a 2 s.  c  om*/
    // TODO Esto debera ir en el metodo que hace getCriteriaSpecification
    // pero como no tiene DetachedCriteria posibilidad de setearle valores
    // para paginacin hubo que hacerlo as.
    if (simpleQuery.getPaging() != null) {
        int firstResult = (simpleQuery.getPaging().getPageNumber() - 1)
                * simpleQuery.getPaging().getResultsPerPage();
        DetachedCriteria idsDetachedCriteria = (DetachedCriteria) criteria;
        idsDetachedCriteria.setProjection(Projections.id());
        Criteria idsCriteria = idsDetachedCriteria.getExecutableCriteria(this.getSession());
        idsCriteria.setCacheable(query.getCacheable());
        idsCriteria.setFirstResult(firstResult);
        idsCriteria.setMaxResults(simpleQuery.getPaging().getResultsPerPage());
        // Se hace primero el select de los IDs y luego el de los objetos en
        // si ya que de otra manera en aquellos casos que haya objetos
        // que tienen colecciones cuenta los mismos varias veces haciendo
        // que se devuelvan menos resultados.
        List<Long> ids = idsCriteria.list();
        DetachedCriteria theCriteria = (DetachedCriteria) getCriteriaSpecification(null, query, false);
        if (ids.isEmpty()) {
            return new ArrayList<T>();
        }
        theCriteria.add(Restrictions.in("id", ids));
        aCriteria = theCriteria.getExecutableCriteria(this.getSession());

    } else {
        aCriteria = ((DetachedCriteria) criteria).getExecutableCriteria(this.getSession());
    }
    aCriteria.setCacheable(query.getCacheable());
    return aCriteria.list();
}

From source file:at.ac.tuwien.ifs.tita.dao.project.TiTAProjectDao.java

License:Apache License

/** {@inheritDoc} **/
@SuppressWarnings("unchecked")
@Override/*from   w  w w.  j a  v a  2s  .  c o m*/
public List<TiTAProject> findProjectsOrderedByName(int maxResult, String orderBy) throws PersistenceException {
    Criteria crit = getSession().createCriteria(TiTAProject.class);

    if (maxResult > 0) {
        crit.setMaxResults(maxResult);
    }

    crit.add(Restrictions.eq("deleted", false));

    crit.addOrder(Order.asc(orderBy));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return crit.list();
}