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.cyclopsgroup.courselist.impl.DefaultCourseListService.java

License:CDDL license

/**
 * Overwrite or implement method findByCode()
 *
 * @see com.cyclopsgroup.courselist.CourseListService#findByCode(java.lang.String, java.lang.String)
 *///from  w  ww  . ja  v  a2s.c o m
public Course findByCode(String prefix, String code) throws Exception {
    Criteria criteria = getHibernateSession().createCriteria(Course.class);
    criteria.add(Expression.eq("prefix", prefix)).add(Expression.eq("courseCode", code))
            .add(Expression.eq("isDisabled", Boolean.FALSE));
    List rs = criteria.setMaxResults(1).list();
    return rs.isEmpty() ? null : (Course) rs.get(0);
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceConfigureDAO.java

License:Open Source License

/**
 * //  w  w  w .  j  a v a 2s .  c  o  m
 * @param sortInfos
 * @param filterInfos
 * @param paginationInfo
 * @return
 */
public List<DeviceConfigureEntity> getAllDeviceConfigureEntities(List<SortParamParser.SortInfo> sortInfos,
        List<FilterParamParser.FilterInfo> filterInfos, PaginationInfo paginationInfo) {
    Session session = null;
    Transaction tx = null;
    List<DeviceConfigureEntity> entityList = new ArrayList<DeviceConfigureEntity>();

    try {
        int offset = paginationInfo.getOffset();
        int limit = paginationInfo.getLimit();
        int pageSize = limit;

        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DeviceConfigureEntity.class);
        BaseDAO.addSortCriteria(criteria, sortInfos);

        BaseDAO.addFilterCriteria(criteria, filterInfos, DeviceConfigureEntity.class);

        criteria.setFirstResult(offset);
        criteria.setMaxResults(pageSize);

        entityList = criteria.list();
        tx.commit();

    } catch (Exception e) {
        logger.warn("Caught exception during get all DeviceConfigureEntities: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get all DeviceConfigureEntities: " + ex);
        }
        throw new AsmManagerInternalErrorException("Error Getting DeviceConfigureEntities",
                "DeviceConfigureDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get all DeviceConfigureEntities: " + ex);
        }
    }

    return entityList;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceDiscoverDAO.java

License:Open Source License

/**
 * //from  w  w w.j av a  2  s  .c  o  m
 * @param sortInfos
 * @param filterInfos
 * @param paginationInfo
 * @return
 */
public List<DeviceDiscoverEntity> getAllDeviceDiscoverEntities(List<SortParamParser.SortInfo> sortInfos,
        List<FilterParamParser.FilterInfo> filterInfos, PaginationInfo paginationInfo) {
    Session session = null;
    Transaction tx = null;
    List<DeviceDiscoverEntity> entityList = new ArrayList<DeviceDiscoverEntity>();

    try {
        int offset = paginationInfo.getOffset();
        int limit = paginationInfo.getLimit();
        int pageSize = limit;

        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DeviceDiscoverEntity.class);
        BaseDAO.addSortCriteria(criteria, sortInfos);

        BaseDAO.addFilterCriteria(criteria, filterInfos, DeviceDiscoverEntity.class);

        criteria.setFirstResult(offset);
        criteria.setMaxResults(pageSize);

        entityList = criteria.list();
        tx.commit();

    } catch (Exception e) {
        logger.warn("Caught exception during get all DeviceDiscoverEntities: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get all DeviceDiscoverEntities: " + ex);
        }
        throw new AsmManagerInternalErrorException("Error Getting DeviceDiscoverEntities", "DeviceDiscoverDAO",
                e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get all DeviceDiscoverEntities: " + ex);
        }
    }

    return entityList;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceGroupDAO.java

License:Open Source License

/**
 * Retrieve all Device Groups//from ww w. j  a v a  2 s  .c o m
 * 
 * @param sortInfos - column name(s) on which the sorting is done
 * @param filterInfos - column name(s) on which the filtering is done
 * @param paginationInfo - pagination information (offset and limit)
 * 
 * @return all the device groups (paginated list)
 * 
 * @throws AsmManagerCheckedException
 */
@SuppressWarnings("unchecked")
public List<DeviceGroupEntity> getAllDeviceGroup(List<SortParamParser.SortInfo> sortInfos,
        List<FilterParamParser.FilterInfo> filterInfos, PaginationParamParser.PaginationInfo paginationInfo)
        throws AsmManagerCheckedException {

    Session session = null;
    Transaction tx = null;
    List<DeviceGroupEntity> result = null;

    try {
        session = _dao._database.getNewSession();
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(DeviceGroupEntity.class);

        if (paginationInfo != null) {
            int offset = paginationInfo.getOffset();
            int pageSize = paginationInfo.getLimit();
            criteria.setFirstResult(offset);
            criteria.setMaxResults(pageSize);
        }

        if (sortInfos != null) {
            addSortCriteria(criteria, sortInfos);
        }

        if (filterInfos != null) {
            addFilterCriteria(criteria, filterInfos, DeviceGroupEntity.class);
        }

        result = (List<DeviceGroupEntity>) criteria.list();
        tx.commit();

    } catch (Exception e) {
        logger.warn("Caught exception during getAllDeviceGroup: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during getAllDeviceGroup: " + ex);
        }
        throw new AsmManagerInternalErrorException("fetch Device Group", "DeviceGroupDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during getAllDeviceGroup: " + ex);
        }
    }

    //if (result == null || result.size() == 0) {
    //    throw new AsmManagerCheckedException(AsmManagerCheckedException.REASON_CODE.RECORD_NOT_FOUND,
    //            AsmManagerMessages.deviceGroupsDataNotFound(""));
    //}

    return result;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DeviceInventoryDAO.java

License:Open Source License

/**
 * Retrieve all from Device Inventory.//from  w  ww.j  a v a  2s.c  o m
 *
 * @return list of entities
 */
@SuppressWarnings("unchecked")
public List<DeviceInventoryEntity> getAllDeviceInventory(List<SortParamParser.SortInfo> sortInfos,
        List<FilterParamParser.FilterInfo> filterInfos, PaginationInfo paginationInfo)
        throws AsmManagerDAOException {

    Session session = null;
    Transaction tx = null;
    List<DeviceInventoryEntity> entityList = new ArrayList<DeviceInventoryEntity>();

    try {
        // NOTE[fcarta] need to at this here so we dont nest the session / transaction
        List<String> refIdsServices = ListUtils.EMPTY_LIST;
        List<String> refIdsGroups = ListUtils.EMPTY_LIST;
        List<String> refIdsAllGroups = ListUtils.EMPTY_LIST;

        boolean spFilterSet = false;
        boolean spGlobalFilterSet = false;
        if (CollectionUtils.isNotEmpty(filterInfos)) {
            for (FilterParamParser.FilterInfo filterInfo : filterInfos) {
                // if there is a service filter then grab the ref id for the device
                if (StringUtils.equals(SERVICE_FILTER_PARAM, filterInfo.getColumnName())) {
                    refIdsServices = getRefIdsOfDevicesByDeploymentIds(filterInfo.getColumnValue());
                } else if (StringUtils.equals(SERVERPOOL_FILTER_PARAM, filterInfo.getColumnName())) {
                    if (filterInfo.getColumnValue()
                            .contains(ServiceTemplateSettingIDs.SERVICE_TEMPLATE_SERVER_POOL_GLOBAL_ID)) {
                        refIdsAllGroups = getRefIdsOfDevicesByGroupIds(filterInfo.getColumnValue());
                        spGlobalFilterSet = true;
                    } else {
                        refIdsGroups = getRefIdsOfDevicesByGroupIds(filterInfo.getColumnValue());
                        spFilterSet = true;
                    }
                }
            }
        }

        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DeviceInventoryEntity.class);
        if (sortInfos != null) {
            BaseDAO.addSortCriteria(criteria, sortInfos);
        }

        if (filterInfos != null) {
            final List<FilterParamParser.FilterInfo> notFound = BaseDAO.addFilterCriteria(criteria, filterInfos,
                    DeviceInventoryEntity.class);
            if (CollectionUtils.isNotEmpty(notFound)) {

                for (FilterParamParser.FilterInfo filterInfo : notFound) {
                    // if this is a filter by service then we need to get the subset of ref ids for devices
                    // only in the deployment id subset passed
                    if (StringUtils.equals(SERVICE_FILTER_PARAM, filterInfo.getColumnName())) {
                        if (CollectionUtils.isNotEmpty(refIdsServices)) {
                            criteria.add(Restrictions.in("refId", refIdsServices));
                        }
                    } else if (StringUtils.equals(SERVERPOOL_FILTER_PARAM, filterInfo.getColumnName())) {
                        if (spGlobalFilterSet) {
                            if (refIdsAllGroups.isEmpty()) {
                                // this means there are no servers in any server pool - all are in Global
                                criteria.add(Restrictions.sqlRestriction("(1=1)"));
                            } else {
                                criteria.add(Restrictions.not(Restrictions.in("refId", refIdsAllGroups)));
                            }
                        } else if (spFilterSet) {
                            if (refIdsGroups.isEmpty()) {
                                criteria.add(Restrictions.sqlRestriction("(1=0)"));
                            } else {
                                criteria.add(Restrictions.in("refId", refIdsGroups));
                            }
                        }
                    } else {
                        criteria.createAlias("deviceInventoryEntity", "deviceInventoryEntityAlias");
                        criteria.add(Restrictions.eq("deviceInventoryEntityAlias.deviceKey",
                                filterInfo.getColumnName()));
                        if (filterInfo.getColumnValue().size() == 1) {
                            criteria.add(Restrictions.eq("deviceInventoryEntityAlias.deviceValue",
                                    filterInfo.getColumnValue().get(0)));
                        } else if (filterInfo.getColumnValue().size() > 1) {
                            criteria.add(Restrictions.in("deviceInventoryEntityAlias.deviceValue",
                                    filterInfo.getColumnValue()));
                        }
                    }
                }
            }
        }

        if (paginationInfo != null) {
            int offset = paginationInfo.getOffset();
            int limit = paginationInfo.getLimit();
            int pageSize = limit;
            criteria.setFirstResult(offset);
            criteria.setMaxResults(pageSize);
        }

        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        entityList = criteria.list();
        this.setFirmwareBasedOnDeployment(entityList);
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get all devices in inventory: " + e, e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get all devices: " + ex);
        }
        throw new AsmManagerDAOException("Caught exception during get all devices in inventory: ", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get all devices: " + ex);
        }
    }

    return entityList;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DiscoveryResultDAO.java

License:Open Source License

/**
 * Retrieve DiscoveryResult based on parent job id.
 * @return the list of discovery results for given parent job id.
 *//*from w  ww  .j a v a 2  s  . c  om*/
@SuppressWarnings("unchecked")
public List<DiscoveryResultEntity> getDiscoveryResult(String parentJobId,
        List<SortParamParser.SortInfo> sortInfos, List<FilterParamParser.FilterInfo> filterInfos,
        PaginationInfo paginationInfo) throws AsmManagerDAOException {

    Session session = null;
    Transaction tx = null;
    List<DiscoveryResultEntity> entityList = new ArrayList<DiscoveryResultEntity>();

    logger.debug("getDiscoveryResult for job: " + parentJobId);

    try {

        int offset = paginationInfo.getOffset();
        int limit = paginationInfo.getLimit();
        int pageSize = limit;

        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DiscoveryResultEntity.class);
        BaseDAO.addSortCriteria(criteria, sortInfos);
        criteria.add(Restrictions.ne("serverType", Server.ServerType.BLADE.value())); // never return blades
        criteria.add(Restrictions.ne("serverType", Server.ServerType.COMPELLENT.value())); // never return compellents which now show up as iDrac7
        List<FilterParamParser.FilterInfo> notFound = BaseDAO.addFilterCriteria(criteria, filterInfos,
                DiscoveryResultEntity.class);

        if (notFound != null && notFound.size() > 0) {
            criteria.createAlias("discoveryResultEntity", "discoveryResultEntityAlias");
            for (FilterParamParser.FilterInfo filterInfo : notFound) {
                criteria.add(
                        Restrictions.eq("discoveryResultEntityAlias.deviceKey", filterInfo.getColumnName()));
                if (filterInfo.getColumnValue().size() == 1) {
                    criteria.add(Restrictions.eq("discoveryResultEntityAlias.deviceValue",
                            filterInfo.getColumnValue().get(0)));
                } else if (filterInfo.getColumnValue().size() > 1) {
                    criteria.add(Restrictions.in("discoveryResultEntityAlias.deviceValue",
                            filterInfo.getColumnValue()));
                }
            }
        }

        //criteria.setFirstResult((pageNumber - 1) * pageSize);
        criteria.setFirstResult(offset);
        criteria.setMaxResults(pageSize);

        entityList = criteria.list();

        // Commit transaction.
        tx.commit();
        logger.debug("getDiscoveryResult for job: " + entityList.size());
    } catch (Exception e) {
        logger.warn("Caught exception during get discovery result for job: " + parentJobId + ", " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get device: " + ex);
        }
        //throw new AsmManagerDAOException("Caught exception during get discovery result for jobId: "    + parentJobId + ", "  + e, e);
        throw new AsmManagerInternalErrorException("Error Getting Discovery result", "DiscoveryResultDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get device: " + ex);
        }
    }

    return entityList;
}

From source file:com.dell.asm.asmcore.asmmanager.db.DiscoveryResultDAO.java

License:Open Source License

/**
 * Retrieve all from DiscoveryResultEntity.
 * @return list of entities//from  ww w  .ja v a  2  s  . c o m
 */
@SuppressWarnings("unchecked")
public List<DiscoveryResultEntity> getAllDiscoveryResult(List<SortParamParser.SortInfo> sortInfos,
        List<FilterParamParser.FilterInfo> filterInfos, PaginationInfo paginationInfo)
        throws AsmManagerDAOException {

    Session session = null;
    Transaction tx = null;
    List<DiscoveryResultEntity> entityList = new ArrayList<DiscoveryResultEntity>();

    try {

        int offset = paginationInfo.getOffset();
        int limit = paginationInfo.getLimit();
        int pageSize = limit;

        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(DiscoveryResultEntity.class);
        BaseDAO.addSortCriteria(criteria, sortInfos);

        List<FilterParamParser.FilterInfo> notFound = BaseDAO.addFilterCriteria(criteria, filterInfos,
                DiscoveryResultEntity.class);

        if (notFound != null && notFound.size() > 0) {
            criteria.createAlias("discoveryResultEntity", "discoveryResultEntityAlias");
            for (FilterParamParser.FilterInfo filterInfo : notFound) {
                criteria.add(Restrictions.eq("discoveryResultEntity.deviceKey", filterInfo.getColumnName()));
                if (filterInfo.getColumnValue().size() == 1) {
                    criteria.add(Restrictions.eq("discoveryResultEntityAlias.deviceValue",
                            filterInfo.getColumnValue().get(0)));
                } else if (filterInfo.getColumnValue().size() > 1) {
                    criteria.add(Restrictions.in("discoveryResultEntityAlias.deviceValue",
                            filterInfo.getColumnValue()));
                }
            }
        }

        //criteria.setFirstResult((pageNumber - 1) * pageSize);
        criteria.setFirstResult(offset);
        criteria.setMaxResults(pageSize);

        entityList = criteria.list();
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get all discovery entities: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get all discovery entities: " + ex);
        }
        throw new AsmManagerDAOException("Caught exception during get all discover entities: ", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get all discover entities: " + ex);
        }
    }

    return entityList;
}

From source file:com.dell.asm.asmcore.asmmanager.db.ServiceTemplateDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<ServiceTemplateEntity> getAllTemplates(List<SortParamParser.SortInfo> sortInfos,
        List<FilterParamParser.FilterInfo> filterInfos, int offset, int pageSize) {

    Session session = null;/* w ww .  ja  v a 2  s. c  o  m*/
    Transaction tx = null;
    List<ServiceTemplateEntity> entityList = new ArrayList<>();

    try {

        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(ServiceTemplateEntity.class);
        BaseDAO.addSortCriteria(criteria, sortInfos);
        List<FilterParamParser.FilterInfo> notFound = BaseDAO.addFilterCriteria(criteria, filterInfos,
                ServiceTemplateEntity.class);

        //criteria.setFirstResult((pageNumber - 1) * pageSize);
        if (offset >= 0)
            criteria.setFirstResult(offset);

        if (pageSize >= 0)
            criteria.setMaxResults(pageSize);

        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        entityList = criteria.list();

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get all templates: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get all templates: " + ex);
        }
        throw new AsmManagerInternalErrorException("Get All templates", "TemplateDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get all devices: " + ex);
        }
    }

    logger.warn("Template DAO get all templates size: " + entityList.size());
    return entityList;
}

From source file:com.dell.asm.asmcore.asmmanager.db.TemplateDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<TemplateEntity> getAllTemplates(List<SortParamParser.SortInfo> sortInfos,
        List<FilterParamParser.FilterInfo> filterInfos, PaginationInfo paginationInfo) {

    Session session = null;//from  www.  j av  a 2 s  . co m
    Transaction tx = null;
    List<TemplateEntity> entityList = new ArrayList<>();

    try {
        int offset = paginationInfo.getOffset();
        int pageSize = paginationInfo.getLimit();

        session = _dao._database.getNewSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(TemplateEntity.class);
        BaseDAO.addSortCriteria(criteria, sortInfos);
        List<FilterParamParser.FilterInfo> notFound = BaseDAO.addFilterCriteria(criteria, filterInfos,
                TemplateEntity.class);

        if (notFound != null && notFound.size() > 0) {
            criteria.createAlias("templateEntity", "templateEntityAlias");
            for (FilterParamParser.FilterInfo filterInfo : notFound) {
                criteria.add(Restrictions.eq("templateEntityAlias.deviceKey", filterInfo.getColumnName()));
                if (filterInfo.getColumnValue().size() == 1) {
                    criteria.add(Restrictions.eq("templateEntityAlias.deviceValue",
                            filterInfo.getColumnValue().get(0)));
                } else if (filterInfo.getColumnValue().size() > 1) {
                    criteria.add(
                            Restrictions.in("templateEntityAlias.deviceValue", filterInfo.getColumnValue()));
                }
            }
        }

        //criteria.setFirstResult((pageNumber - 1) * pageSize);
        criteria.setFirstResult(offset);
        criteria.setMaxResults(pageSize);
        criteria.setFetchMode("policyRefEntities", FetchMode.SELECT);

        entityList = criteria.list();

        //            Collection<PolicyRefEntity> dummy;
        //            for (TemplateEntity parm : entityList) {
        //              dummy = parm.getPolicyRefEntities();
        //              dummy.isEmpty();
        //            }

        // workaround for dupliacate templates
        //entityList.addAll(buildUniqueList(criteria.list()));

        // Commit transaction.
        tx.commit();
    } catch (Exception e) {
        logger.warn("Caught exception during get all templates: " + e);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
            logger.warn("Unable to rollback transaction during get all templates: " + ex);
        }
        throw new AsmManagerInternalErrorException("Get All templates", "TemplateDAO", e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (Exception ex) {
            logger.warn("Unable to close session during get all devices: " + ex);
        }
    }

    logger.warn("Template DAO get all templates size: " + entityList.size());
    return entityList;
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.HibernateApplicationDao.java

License:Mozilla Public License

@Override
public List<Application> getSearchResults(Integer teamId, String searchString, int number, int page,
        Set<Integer> appIds, Set<Integer> teamIds) {

    Criteria criteria = getSearchAppCriteria(teamId, searchString);
    criteria.addOrder(Order.asc("name"));
    addFiltering(criteria, teamIds, appIds);
    return (List<Application>) criteria.setMaxResults(number).setFirstResult((page - 1) * number).list();
}