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:cn.dao.MedicinePurchaseDao.java

public List<Object> queryByBatchNumber(MedicinePurchase vo, PageResult page) {
    Session session = this.getSessionFactory().getCurrentSession();
    Map<String, Object> properties = new HashMap<>();
    StringBuffer hql = new StringBuffer();
    hql.append("select u.license_number,u.medicine_name,t.cost_price,t.number");
    hql.append(" from medicine_purchase t");
    hql.append(" left join medicine_info u");
    hql.append(" on t.medicine_id = u.id");
    hql.append(" where t.batch_number = :batchNumber");
    hql.append(" order by t.purchase_date desc");
    Query query = session.createSQLQuery(hql.toString());
    properties.put("batchNumber", vo.getBatchNumber());
    query.setProperties(properties);//  w  w  w. j av  a2  s . c  om
    if (page != null) {
        int items = getPageCount(session, hql.toString(), properties);
        page.setItems(items);
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResult());
    }
    return query.list();
}

From source file:cn.dao.MedicineSalesDao.java

@SuppressWarnings("unchecked")
public List<Object> query(MedicineSales vo, PageResult page) {
    Session session = this.getSessionFactory().getCurrentSession();
    Map<String, Object> properties = new HashMap<>();
    StringBuffer hql = new StringBuffer();
    hql.append("select t.sale_batch_number,t.sale_date,u.name");
    hql.append(" from medicine_sales t");
    hql.append(" left join user_info u");
    hql.append(" on t.user_id = u.id");
    hql.append(" group by t.sale_batch_number,t.sale_date,u.name");
    hql.append(" order by t.sale_date desc");
    Query query = session.createSQLQuery(hql.toString());
    query.setProperties(properties);/* w  ww .  j  a va2 s  .co m*/
    if (page != null) {
        int items = getPageCount(session, hql.toString(), properties);
        page.setItems(items);
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResult());
    }
    return query.list();
}

From source file:cn.dao.MedicineSalesDao.java

public List<Object> queryByBatchNumber(MedicineSales vo, PageResult page) {
    Session session = this.getSessionFactory().getCurrentSession();
    Map<String, Object> properties = new HashMap<>();
    StringBuffer hql = new StringBuffer();
    hql.append("select u.license_number,u.medicine_name,t.sale_price,t.sale_number");
    hql.append(" from medicine_sales t");
    hql.append(" left join medicine_storehouse s");
    hql.append(" on t.medicine_store_house_id = s.id");
    hql.append(" left join medicine_info u");
    hql.append(" on s.medicine_id = u.id");
    hql.append(" where t.sale_batch_number = :batchNumber");
    hql.append(" order by t.sale_date desc");
    Query query = session.createSQLQuery(hql.toString());
    properties.put("batchNumber", vo.getSaleBatchNumber());
    query.setProperties(properties);// ww  w .j  a  va  2  s .c o  m
    if (page != null) {
        int items = getPageCount(session, hql.toString(), properties);
        page.setItems(items);
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResult());
    }
    return query.list();
}

From source file:cn.dayuanzi.dao.BaseDao.java

License:Apache License

/**
 * //from   w w w .  java2 s  . c o m
 * 
 * @param hql
 * @param current_page
 * @param max_num
 * @param map
 * @return
 */
public List<T> findForPage(String hql, int current_page, int max_num, Map<String, Object> map) {
    List<T> result = null;
    if (current_page <= 0) {
        current_page = 1;
    }
    if (max_num > 20) {
        max_num = 20;
    } else if (max_num <= 0) {
        max_num = 1;
    }
    try {
        if (!hql.contains("where")) {
            hql = hql.concat(" where 1=1 ");
        }
        if (map != null) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                hql = hql.concat(" and " + key + " =:" + key);
            }
        }
        Query query = this.getSession().createQuery(hql);
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            query.setParameter(key, map.get(key));
        }
        query.setFirstResult(((current_page - 1)) * max_num);
        query.setMaxResults(max_num);
        result = query.list();
    } catch (RuntimeException re) {
        throw re;
    }
    return result;
}

From source file:cn.hxh.springside.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Query,./*w ww .ja  v a  2 s .c  o m*/
 */
protected Query setPageParameterToQuery(final Query q, final PageRequest pageRequest) {
    q.setFirstResult(pageRequest.getOffset());
    q.setMaxResults(pageRequest.getPageSize());
    return q;
}

From source file:cn.newtouch.util.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Query,./*from   www  .jav a  2s . c  o m*/
 */
protected Query setPageParameterToQuery(final Query q, final Page<T> page) {

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

    // hibernatefirstResult??0
    q.setFirstResult(page.getFirst() - 1);
    q.setMaxResults(page.getPageSize());
    return q;
}

From source file:cn.newtouch.util.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Query,.//w w w  .  j a  va 2 s .  c  om
 */
protected Query setPageParameterToQuery(final Query q, final Page<T> page) {

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

    //hibernatefirstResult??0
    q.setFirstResult(page.getFirst() - 1);
    q.setMaxResults(page.getPageSize());
    return q;
}

From source file:com.abiquo.abiserver.persistence.dao.metering.hibernate.MeterDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  www. jav a2 s.  co  m
public List<MeterHB> findAllByFilter(final HashMap<String, String> filter, final List<String> performedbyList,
        final Integer numrows, final UserHB user) throws PersistenceException {
    Integer numberOfParameters = 0;
    List<MeterHB> listOfMeters = new ArrayList<MeterHB>();
    StringBuilder stringQuery = new StringBuilder(
            "from com.abiquo.abiserver.business.hibernate.pojohb.metering.MeterHB ");

    // Create Dates
    String fromDateInit = new Timestamp(0).toString();
    String fromdateFilter;
    if (filter.containsKey("datefrom")) {
        fromdateFilter = new Timestamp(Long.valueOf(filter.get("datefrom")) * 1000).toString();
    } else {
        fromdateFilter = fromDateInit;
    }

    String toDateEnd = new Timestamp(new Date().getTime()).toString();
    String todateFilter;
    if (filter.containsKey("dateto")) {
        // We sum another day to include both filters
        todateFilter = new Timestamp((Long.valueOf(filter.get("dateto")) + 86400) * 1000).toString();
    } else {
        todateFilter = toDateEnd;
    }

    stringQuery.append("where timestamp between '").append(fromdateFilter).append("' and '")
            .append(todateFilter).append("' and timestamp > '").append(user.getCreationDate().toString())
            .append("'");

    // create all the filters from the HashMap information. Due all of them are included in the
    // query, we need to
    // insert '%' for all the filters not included in the hashmap
    if (filter.containsKey("datacenter")) {
        stringQuery.append(" and datacenter like '%" + filter.get("datacenter") + "%'");
    }
    if (filter.containsKey("rack")) {
        stringQuery.append(" and rack like '%" + filter.get("rack") + "%'");
    }
    if (filter.containsKey("physicalmachine")) {
        stringQuery.append(" and physicalMachine like '%" + filter.get("physicalmachine") + "%'");
    }
    if (filter.containsKey("storagesystem")) {
        stringQuery.append(" and storagesystem like '%" + filter.get("storagesystem") + "%'");
    }
    if (filter.containsKey("storagepool")) {
        stringQuery.append(" and storagepool like '%" + filter.get("storagepool") + "%'");
    }
    if (filter.containsKey("volume")) {
        stringQuery.append(" and volume like '%" + filter.get("volume") + "%'");
    }
    if (filter.containsKey("network")) {
        stringQuery.append(" and network like '%" + filter.get("network") + "%'");
    }
    if (filter.containsKey("subnet")) {
        stringQuery.append(" and subnet like '%" + filter.get("subnet") + "%'");
    }
    if (filter.containsKey("enterprise")) {
        stringQuery.append(" and enterprise like '%" + replaceApostrophe(filter.get("enterprise")) + "%'");
    }
    if (filter.containsKey("user")) {
        stringQuery.append(" and user like '%" + filter.get("user") + "%'");
    }
    if (filter.containsKey("virtualdatacenter")) {
        stringQuery.append(" and virtualDataCenter like '%" + filter.get("virtualdatacenter") + "%'");
    }
    if (filter.containsKey("virtualapp")) {
        stringQuery.append(" and virtualApp like '%" + filter.get("virtualapp") + "%'");
    }
    if (filter.containsKey("virtualmachine")) {
        stringQuery.append(" and virtualmachine like '%" + filter.get("virtualmachine") + "%'");
    }
    if (filter.containsKey("severity")) {
        stringQuery.append(" and severity like '" + filter.get("severity") + "%'");
    }
    if (filter.containsKey("performedby")) {
        stringQuery.append(" and performedby like '%" + filter.get("performedby") + "%'");
    }
    if (filter.containsKey("actionperformed")) {
        stringQuery.append(" and actionperformed like '" + filter.get("actionperformed") + "%'");
    }
    if (filter.containsKey("component")) {
        stringQuery.append(" and component like '%" + filter.get("component") + "%'");
    }

    // if (role != Role.SYS_ADMIN)
    if (!SecurityService.isCloudAdmin(user.getRoleHB().toPojo())) {
        if (!SecurityService.hasPrivilege(SecurityService.EVENTLOG_VIEW_ENTERPRISE, user.getRoleHB().toPojo())
                && !SecurityService.hasPrivilege(SecurityService.EVENTLOG_VIEW_ALL,
                        user.getRoleHB().toPojo())) {
            stringQuery.append(" and idUser = " + user.getIdUser());
        }

        if (performedbyList != null && !performedbyList.isEmpty()) {
            // performedby filter
            stringQuery.append(" and performedby in (");
            boolean firstentry = true;
            for (String currentString : performedbyList) {
                if (firstentry == false) {
                    stringQuery.append(",");
                }
                stringQuery.append("'" + currentString + "'");
                if (firstentry == true) {
                    firstentry = false;
                }
            }
            stringQuery.append(")");
        }
    }
    // delete last ','
    stringQuery.append(" order by timestamp desc");

    // Setting the query
    Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();
    Query meterQuery = session.createQuery(stringQuery.toString());
    meterQuery.setMaxResults(numrows);

    listOfMeters = meterQuery.list();

    return listOfMeters;
}

From source file:com.abiquo.server.core.cloud.VirtualMachineDAO.java

License:Open Source License

public List<VirtualMachine> findVirtualMachinesByVirtualAppliance(final Integer vappId, Integer startwith,
        Integer limit, final String filter, final OrderByEnum orderby, final boolean asc) {
    // List<VirtualMachine> vmList = null;

    // TypedQuery<VirtualMachine> query =
    // getEntityManager().createNamedQuery("VIRTUAL_MACHINE.BY_VAPP", VirtualMachine.class);
    // query.setParameter("vapp_id", vappId);
    // vmList = query.getResultList();
    String orderBy = defineOrderBy(orderby.getColumnHQL(), asc);
    Query query = getSession().getNamedQuery("VIRTUAL_MACHINE.BY_VAPP");

    String req = query.getQueryString() + orderBy;
    // Add order filter to the query
    Query queryWithOrder = getSession().createQuery(req);
    queryWithOrder.setInteger("vapp_id", vappId);
    queryWithOrder.setString("filterLike", filter.isEmpty() ? "%" : "%" + filter + "%");

    Integer size = queryWithOrder.list().size();

    // Limit 0 means no size filter
    if (limit == 0) {
        limit = size;// w  w w .j  a va2s.co m
        startwith = 0;
    }

    queryWithOrder.setFirstResult(startwith);
    queryWithOrder.setMaxResults(limit);

    PagedList<VirtualMachine> vmList = new PagedList<VirtualMachine>(queryWithOrder.list());
    vmList.setTotalResults(size);
    vmList.setPageSize(limit > size ? size : limit);
    vmList.setCurrentElement(startwith);

    return vmList;
}

From source file:com.abiquo.server.core.infrastructure.DatacenterDAO.java

License:Open Source License

public List<Enterprise> findEnterprisesByDatacenters(final Datacenter datacenter, final Integer firstElem,
        final Integer numElem, final Boolean network) {

    // Get the query that counts the total results.
    Query finalQuery;
    if (network) {
        finalQuery = getSession().createQuery(BY_ENT_WITH_NETWORK);
    } else {//w ww .java  2s .c  o  m
        finalQuery = getSession().createQuery(BY_ENT);
    }
    finalQuery.setParameter("datacenter_id", datacenter.getId());
    Integer totalResults = finalQuery.list().size();

    Integer Start = firstElem;
    if (totalResults < firstElem) {
        Start = totalResults - numElem;
    }
    // Get the list of elements
    finalQuery.setFirstResult(Start);
    finalQuery.setMaxResults(numElem);

    PagedList<Enterprise> entList = new PagedList<Enterprise>(finalQuery.list());
    entList.setTotalResults(totalResults);
    entList.setPageSize(numElem);
    entList.setCurrentElement(firstElem);

    return entList;
}