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.aptech.model.ProductDao.java

public List pagingWithCategoryId(Integer pageNumber, Integer perPage, Integer catId) {
    List objects = null;/*  www  . j  a  v a 2 s .  com*/
    try {
        startOperation2();
        Query query = session
                .createQuery("from " + Product.class.getName() + " p where p.category.id = " + catId);
        query.setMaxResults(perPage);
        query.setFirstResult(pageNumber);
        objects = query.list();
        tx.commit();
    } catch (HibernateException e) {
        handleException(e);
    } finally {
        HibernateFactory.close(session);
    }
    return objects;
}

From source file:com.archive.spring.dao.impl.ImageDAOImpl.java

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();//w  w  w .j  ava2s.c  o  m

    return liste;
}

From source file:com.archive.spring.dao.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();/* ww  w . ja v  a  2  s  . co m*/

    return liste;
}

From source file:com.asecurity.eventslogdb.AccessEvents.java

private List<ApacseventsCha> getApacseventsCha(String hql, int resCnt, boolean useNativeSQL) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    List<ApacseventsCha> aeCha = null;
    Transaction tx = null;//w  w  w  . ja  v  a 2  s.c o m
    try {
        tx = session.beginTransaction();
        Query q;
        if (useNativeSQL) {
            q = session.createSQLQuery(hql);
            q.setResultTransformer(Transformers.aliasToBean(ApacseventsCha.class));
        } else {
            q = session.createQuery(hql);
        }
        q.setMaxResults(resCnt);
        aeCha = (List<ApacseventsCha>) q.list();
    } catch (Exception e) {
        System.out.println(e.toString());
    } finally {
        if (tx != null) {
            tx.rollback();
        }
        if (session.isOpen()) {
            session.close();
        }
    }
    return aeCha;
}

From source file:com.asociate.dao.DirectorioDAO.java

/**
 *
 * @param idAsociacion//from  ww w.  ja v  a  2  s  . c  om
 * @return
 */
public boolean hasDirectorioAsociacion(Long idAsociacion) {
    Session sesion = HibernateUtil.getSessionFactory().openSession();
    Directorio dir = null;
    try {
        Query qu = sesion.createQuery(
                "Select D from Directorio D where D.idDirectorio = (Select A.idDirectorio from Asociacion A where A.idAsociacion=:idAsoc)");
        qu.setParameter("idAsoc", idAsociacion);
        qu.setMaxResults(1).getFirstResult();

    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        sesion.flush();
        sesion.close();
    }
    return dir != null;

}

From source file:com.asociate.dao.DirectorioDAO.java

/**
 *
 * @param idPersona/*from w  w  w . java  2 s  . co m*/
 * @return
 */
public boolean hasDirectorioPersona(Long idPersona) {
    Session sesion = HibernateUtil.getSessionFactory().openSession();
    Directorio dir = null;
    try {
        Query qu = sesion.createQuery(
                "Select D from Directorio D where D.idDirectorio = (Select A.idDirectorio from Asociacion A where A.idAsociacion in( Select SO from Socio SO where SO.idPersona = :idPer))");
        qu.setParameter("idPer", idPersona);
        qu.setMaxResults(1).getFirstResult();

    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        sesion.flush();
        sesion.close();
    }
    return dir != null;
}

From source file:com.autentia.wuija.persistence.impl.hibernate.HibernateDao.java

License:Open Source License

private void setPagination(final Query query, final int firstResult, final int maxResults) {
    query.setFirstResult(firstResult);//from   w w w . j  av  a2 s  . c o m
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }
}

From source file:com.autentia.wuija.persistence.impl.hibernate.HibernateDao.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override// ww  w.  j a  v a2s.  c om
public <T> Pair<List<T>, Long> findAndCountByNamedQueryWithInStatements(final String namedQuery,
        final String countNamedQuery, final int firstResult, final int maxResults, final Object... params) {
    return (Pair<List<T>, Long>) getHibernateTemplate().execute(new HibernateCallback() {

        @Override
        public Object doInHibernate(Session session) throws HibernateException {
            final Query countQuery = session.getNamedQuery(countNamedQuery);
            addParamsToQueryCheckingIfIsListType(countQuery, params);
            final Long countResult = (Long) countQuery.list().get(0);
            final Query query = session.getNamedQuery(namedQuery);
            addParamsToQueryCheckingIfIsListType(query, params);
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
            return new Pair<List<T>, Long>(query.list(), countResult);
        }

    });
}

From source file:com.autentia.wuija.persistence.impl.hibernate.HibernateDao.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//ww w.  j a va  2 s.c o  m
public <T> Pair<List<T>, Long> findAndCountByHqlQueryWithInStatements(final String hqlQuery,
        final int firstResult, final int maxResults, final Object... params) {
    return (Pair<List<T>, Long>) getHibernateTemplate().execute(new HibernateCallback() {

        @Override
        public Object doInHibernate(Session session) throws HibernateException {
            final Query query = session.createQuery(hqlQuery);
            addParamsToQueryCheckingIfIsListType(query, params);
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
            final List<T> result = query.list();
            return new Pair<List<T>, Long>(result, Long.valueOf(result.size()));
        }

    });

}

From source file:com.bancomat.springmvc.dao.MovimentiDao.java

public static ArrayList<Movimenti> getMovimenti(int id) {
    ArrayList<Movimenti> movimenti = new ArrayList<Movimenti>();
    session = HibernateUtil.getSessionFactory().openSession();
    Transaction tsc = session.beginTransaction();
    String select = "from Movimenti m where idUtente = :id order by m.data desc ";

    Query qry = session.createQuery(select);
    qry.setInteger("id", id);
    qry.setMaxResults(10);
    movimenti = (ArrayList<Movimenti>) qry.list();

    return movimenti;
}