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:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public List getTenantRoles(ExecutionContext context, final Set tenantIds, final String name,
        final int firstResult, final int maxResults) {
    List roleList = (List) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createTenantRolesCriteria(session, tenantIds, name);
            if (firstResult >= 0) {
                criteria.setFirstResult(firstResult);
            }//from   w  w w  .  ja v  a 2 s  . co m
            if (maxResults > 0) {
                criteria.setMaxResults(maxResults);
            }
            return criteria.list();
        }
    });
    return convertRoleListToDtoList(roleList);
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

public List findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults)
        throws Exception {

    return (List) doHibernate(new HibernateCallback() {
        public Object execute(Session session) throws HibernateException {
            Criteria executableCriteria = criteria.getExecutableCriteria(session);
            prepareCriteria(executableCriteria);
            if (firstResult >= 0) {
                executableCriteria.setFirstResult(firstResult);
            }//  w w  w . j  ava  2s  .co  m
            if (maxResults > 0) {
                executableCriteria.setMaxResults(maxResults);
            }
            return executableCriteria.list();
        }
    });
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

public List findByExample(final Object exampleEntity, final int firstResult, final int maxResults)
        throws Exception {

    return (List) doHibernate(new HibernateCallback() {
        public Object execute(Session session) throws HibernateException {
            Criteria executableCriteria = session.createCriteria(exampleEntity.getClass());
            executableCriteria.add(Example.create(exampleEntity));
            prepareCriteria(executableCriteria);
            if (firstResult >= 0) {
                executableCriteria.setFirstResult(firstResult);
            }//  w ww  .  j a v a  2  s . com
            if (maxResults > 0) {
                executableCriteria.setMaxResults(maxResults);
            }
            return executableCriteria.list();
        }
    });
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

/**
 * Prepare the given Criteria object, applying cache settings and/or a
 * transaction timeout./*from   w w  w . j av  a 2  s.  c o  m*/
 * 
 * @param criteria
 *            the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionProviderHolder#applyTransactionTimeout
 */
protected void prepareCriteria(Criteria criteria) {
    if (isCacheQueries()) {
        criteria.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            criteria.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        criteria.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        criteria.setMaxResults(getMaxResults());
    }
}

From source file:com.jeroensteenbeeke.hyperion.data.HibernateDAO.java

License:Open Source License

@Override
public List<T> findByFilter(SearchFilter<T> filter, long offset, long count) {
    Criteria crit = applyOrderBy(createCriteria(filter), filter);

    if (crit != null) {
        crit.setMaxResults((int) count);
        crit.setFirstResult((int) offset);

        return crit.list();
    }/*from ww  w. j a  v a2s. c o  m*/

    return new LinkedList<T>();
}

From source file:com.jeysan.modules.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Criteria,./*from  w w  w.j a  v a2s.  c o  m*/
 */
protected Criteria setPageParameterToCriteria(final Criteria c, final Page<T> page) {

    //Assert.isTrue(page.getPageSize() > 0, "Page Size must larger than zero");

    //hibernatefirstResult??0
    if (page.getPageSize() != -1) {
        c.setFirstResult(page.getFirst() - 1);
        c.setMaxResults(page.getPageSize());
    }
    if (page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length,
                "???,????");

        return HibernateUtils.checkCriteria4SubOrder(c, orderArray, orderByArray);
    }
    //logger.debug("c:::"+c);
    return c;
}

From source file:com.jfootball.dao.hibernate.PlayerDaoImpl.java

License:Open Source License

/**
 * Method to get a player/*from   w  w  w. j  a  v a  2  s . c om*/
 * 
 * @param playerId
 *            the playerId id
 * @return the players found
 */
public HashMap<String, Object> getNextId(Long teamId, Integer counter) {
    logger.info("Next id team " + teamId + " counter " + counter);

    HashMap<String, Object> map = new HashMap<String, Object>();

    Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
    Criteria criteria = session.createCriteria(Player.class);
    criteria.add(Restrictions.eq("team.id", teamId));
    criteria.addOrder(Order.asc("position"));
    criteria.addOrder(Order.asc("number"));
    criteria.addOrder(Order.asc("dateOfBirth"));

    criteria.setFirstResult(counter);
    criteria.setMaxResults(1);

    Long nextId = 0L;
    Player player = null;

    if (criteria.uniqueResult() != null) {
        player = (Player) criteria.uniqueResult();
        nextId = player.getId();

        map.put("counter", counter);
        map.put("id", nextId);
        map.put("player", player.getFirstName() + " " + player.getLastName());
    } else {
        nextId = -1L;

        map.put("counter", counter);
        map.put("id", -1L);
        map.put("player", null);
    }

    logger.info("Next id returned");

    return map;
}

From source file:com.jfootball.dao.hibernate.PlayerDaoImpl.java

License:Open Source License

/**
 * Method to get a player/*from w ww .  ja  v a 2s.  co m*/
 * 
 * @param playerId
 *            the playerId id
 * @return the players found
 */
public HashMap<String, Object> getPrevId(Long teamId, Integer counter) {
    logger.info("Prev id team " + teamId + " counter " + counter);

    HashMap<String, Object> map = new HashMap<String, Object>();

    Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
    Criteria criteria = session.createCriteria(Player.class);
    criteria.add(Restrictions.eq("team.id", teamId));
    criteria.addOrder(Order.desc("position"));
    criteria.addOrder(Order.desc("number"));
    criteria.addOrder(Order.desc("dateOfBirth"));

    criteria.setFirstResult(counter);
    criteria.setMaxResults(1);

    Long prevId = 0L;
    Player player = null;

    if (criteria.uniqueResult() != null) {
        player = (Player) criteria.uniqueResult();
        prevId = player.getId();

        map.put("counter", counter);
    } else {
        map.put("counter", -1);
    }

    map.put("id", prevId);
    map.put("player", player.getFirstName() + " " + player.getLastName());

    logger.info("Prev id returned");

    return map;
}

From source file:com.jscompany.ebsystem.lazymodels.BaseLazyDataModel.java

public void criteriaPageSetup(Criteria crit, int first, int pageSize) {
    try {/*from ww w . j av a 2  s  . c  o  m*/
        crit.setFirstResult(first);
        crit.setMaxResults(pageSize);
    } catch (Exception e) {
        Logger.getLogger(BaseLazyDataModel.class.getName()).log(Level.SEVERE, null, e);
    }
}

From source file:com.jubination.model.dao.AdminDAOImpl.java

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public Object readPropertyList(Object entity, String listType) {
    Admin admin = (Admin) entity;/*from   w  ww  . jav  a 2s  . co  m*/
    List list = null;
    switch (listType) {
    case "Inbox":
        session = getSessionFactory().getCurrentSession();

        admin = (Admin) session.get(Admin.class, admin.getUsername());

        Criteria criteria = session.createCriteria(MailMessage.class, "msg");
        criteria.add(Restrictions.eq("msg.receiver.username", admin.getUsername()));
        criteria.addOrder(Order.desc("messageId"));

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        criteria.setFirstResult(0);
        criteria.setMaxResults(10);
        list = criteria.list();

        break;
    case "Sent":
        session = getSessionFactory().getCurrentSession();

        admin = (Admin) session.get(Admin.class, admin.getUsername());

        criteria = session.createCriteria(MailMessage.class, "msg");
        criteria.add(Restrictions.eq("msg.sender.username", admin.getUsername()));
        criteria.addOrder(Order.desc("messageId"));

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        criteria.setFirstResult(0);
        criteria.setMaxResults(10);
        list = criteria.list();

        break;
    default:
        System.out.println("Not an option");
        admin = null;
        break;
    }

    return (T) list;
}