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.Bean.PostinfoHelper.java

public List<Post> getAllPost(int page) {
    List<Post> postList = new ArrayList<Post>();
    try {/*  ww  w  .j a va 2  s  .  co  m*/
        Query q = session.createQuery("from Post as p order by p.creationTime desc ");
        maxPage = q.list().size() / itemsPerPage;
        q.setFirstResult(page * itemsPerPage);
        q.setMaxResults(itemsPerPage);
        postList = (List<Post>) q.list();
        return postList;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.Bean.PostinfoHelper.java

public List<Post> getMyPost(int page, int userid) {
    List<Post> postList = new ArrayList<Post>();
    try {//  w  w  w. j  a v a2s . c  om
        Query q = session
                .createQuery("from Post as p where p.userId =" + userid + "order by p.creationTime desc");
        maxPage = q.list().size() / itemsPerPage;
        q.setFirstResult(page * itemsPerPage);
        q.setMaxResults(itemsPerPage);
        postList = (List<Post>) q.list();
        return postList;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.booleanworks.kryptopterus.application.MainHibernateUtil.java

License:Apache License

public List<Object> executeQuery(Session session, String query, Map<String, Object> parameters, int offset,
        int maxResults) {
    System.out.println("com.booleanworks.kryptopterus.application.MainHibernateUtil.executeQuery()");
    System.out.println("session  => " + session.hashCode());

    if (session == null || !session.isConnected() || !session.isOpen()) {
        session = this.getResidentSession();
    }//  www .j ava  2 s .c om

    List<Object> result = new ArrayList<Object>();

    Query q1 = session.createQuery(query);
    for (String paramName : parameters.keySet()) {
        q1.setParameter(paramName, parameters.get(paramName));
    }
    q1.setMaxResults(maxResults);
    q1.setFirstResult(offset);
    result = q1.list();

    return result;
}

From source file:com.booleanworks.kryptopterus.application.MainHibernateUtil.java

License:Apache License

public Object findOrNull(Session session, String query, Map<String, Object> parameters) {
    System.out.println("com.booleanworks.kryptopterus.application.MainHibernateUtil.findOrNull()");
    System.out.println("session  => " + session.hashCode());
    List<Object> result = new ArrayList<Object>();

    Query q1 = session.createQuery(query);
    for (String paramName : parameters.keySet()) {
        q1.setParameter(paramName, parameters.get(paramName));
    }/*  www . ja va2s . c  o m*/
    q1.setMaxResults(1);
    q1.setFirstResult(0);
    result = q1.list();

    if (result.size() == 1) {
        return result.get(0);
    }

    return null;
}

From source file:com.bplow.look.bass.dao.HibernateDao.java

License:Apache License

/**
 * ?Query,.// w w  w  .  j a  va  2s  .  com
 */
protected Query setPageParameter(final Query q, final Page<T> page) {
    //hibernatefirstResult??0
    q.setFirstResult(page.getFirst() - 1);
    q.setMaxResults(page.getPageSize());
    return q;
}

From source file:com.bplow.look.bass.dao.HibernateDao.java

License:Apache License

/**
 * hibernate //from w w w. j  a  va 2  s . co m
 * 
 */
public IPagination queryForPagination(String hql, String hqlCount, int firstResult, int maxResults)
        throws DataAccessException {
    IPagination pagination = new SimplePagination(firstResult, maxResults);
    String fromHql = hql;
    //select??order by???count,?.
    fromHql = "from " + StringUtils.substringAfter(fromHql, "from");
    fromHql = StringUtils.substringBefore(fromHql, "order by");

    Query q = createQuery(hql);
    Long totalCount = 0L;
    if (StringUtils.isNotEmpty(hqlCount)) {
        //totalCount =countHqlResult(hqlCount);
        totalCount = (Long) this.getSession().createQuery(hqlCount).uniqueResult();
    } else
        totalCount = (Long) this.getSession().createQuery(" select count(*) " + fromHql).uniqueResult();
    ;

    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    List result = q.list();
    pagination.setAllCount(Integer.parseInt(totalCount.toString()));
    pagination.setResults(result);

    return pagination;
}

From source file:com.britesnow.snow.web.db.hibernate.HibernateDaoHelperImpl.java

License:Apache License

/**
 * For now, this is transactional to support Postgres (if the query fail then postgres lock all the ss
 * @see com.britesnow.snow.web.db.hibernate.HibernateDaoHelper#find(int, int, java.lang.String, java.lang.Object[])
 *//*from   w  ww. j  a  v a2 s  . c  o m*/
@SuppressWarnings("unchecked")
public List<? extends Object> find(int pageIdx, int pageSize, String query, Object... values) {
    Session session = getSession();
    try {
        Query q = session.createQuery(query);

        if (values != null) {
            int i = 0;
            for (Object val : values) {
                q.setParameter(i++, val);
            }
        }
        q.setFirstResult(pageIdx * pageSize);
        q.setMaxResults(pageSize);

        return q.list();
    } catch (Throwable e) {
        throw new SnowHibernateException(e);
    }
}

From source file:com.bullx.cacdata.CACData.java

License:Open Source License

@SuppressWarnings("unchecked")
private DOMElement getMonitorData() {
    DOMElement monitorNode = new DOMElement("monitordata");

    // sensor?//from w  w  w . j a  va  2  s .  co  m
    int dataNumber = 0;

    ConfigIedDAO iedDAO = new ConfigIedDAO();
    List<ConfigIed> list = iedDAO.findAll();

    monitorNode.setAttribute("cacid", list.get(0).getConfigCac().getCacId());
    HashMap<String, String> objectMapper = new HashMap<String, String>();
    //add mappers the first is the ied class, the second is the query string
    objectMapper.put("SIML", "DataSiml");
    objectMapper.put("SPDC", "DataSpdc");
    objectMapper.put("MMXN", "DataMmxn");
    objectMapper.put("ZSAR", "DataZsar");
    objectMapper.put("SIMG", "DataSimg");
    objectMapper.put("SENV", "DataSenv");

    for (int i = 0; i < list.size(); i++) {
        ConfigIed ied = list.get(i);

        String objectString = objectMapper.get(ied.getLnClass());
        String queryString = "from " + objectString
                + " as inst where inst.lnInst=? order by inst.dataTime desc";
        Query query = iedDAO.getSession().createQuery(queryString);
        query.setInteger(0, ied.getLnInst());
        query.setFirstResult(0);
        query.setMaxResults(1);
        List<Node> thizNodes = getDataNodes(ied.getLnClass(), query, ied);
        for (int j = 0; j < thizNodes.size(); j++, dataNumber++) {
            monitorNode.add(thizNodes.get(j));
        }
    }

    monitorNode.setAttribute("datanodenum", Integer.toString(dataNumber));

    return monitorNode;
}

From source file:com.candy.db.FundamentalDataProc.java

/**
 * read records from table//from   w w w . j av  a  2 s . co m
 * @param symbol
 * @param count
 * @param qtv
 * @return 
 */
public ArrayList<FundamentalDataRec> readDataByCount(String symbol, int type, int count, boolean qtv) {
    try {
        begin();
        Query q = null;
        if (qtv)
            q = getSession().createQuery(
                    "from Fundamentaldata where symbol = :symbol and quarter != 0 and type = :type order by year desc");
        else
            q = getSession().createQuery(
                    "from Fundamentaldata where symbol = :symbol and quarter = 0 and type = :type order by year desc");
        q.setString("symbol", symbol);
        q.setInteger("type", type);
        if (count != 0) {
            q.setFirstResult(0);
            q.setMaxResults(count);
        }
        List<Fundamentaldata> results = q.list();
        commit();
        if (results.isEmpty()) {
            return null;
        } else {
            ArrayList<FundamentalDataRec> retLst = new ArrayList();
            for (Fundamentaldata fdata : results) {
                FundamentalDataRec rec = new FundamentalDataRec();
                rec.year = fdata.getId().getYear();
                rec.quarter = fdata.getId().getQuarter();
                rec.setType(fdata.getId().getType());
                for (int i = 0; i < methodGetValueLst.length; i++) {
                    try {
                        Double value = (Double) methodGetValueLst[i].invoke(fdata); // return double
                        String name = (String) methodGetNameLst[i].invoke(fdata);
                        if (name != null) {
                            rec.setNameValue(name, value);
                        }
                        //                            else {  // TODO
                        //                                System.out.println("ERROR - fundamental name is null " + i);
                        //                            }
                    } catch (Exception e) {
                        System.out.println("ERROR - unable to get column value/name = " + i);
                    }
                }
                retLst.add(rec);
            }
            return retLst;
        }
    } catch (HibernateException e) {
        return null;
    }
}

From source file:com.candy.db.StockEodProc.java

/**
 * get last available record's date/*ww w . j a va2  s  .  c om*/
 * @param symbol
 * @return 
 */
public Calendar getHisStockDataLastAvaDate(String symbol) {
    try {
        begin();
        Query q = getSession().createQuery("from Stockeod where symbol = :symbol order by sdate desc");
        q.setString("symbol", symbol);
        q.setFirstResult(0);
        q.setMaxResults(1);

        Stockeod stockEod = (Stockeod) q.uniqueResult();
        commit();

        if (stockEod != null) {
            Date date = stockEod.getId().getSdate();
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal;
        } else {
            return null;
        }
    } catch (HibernateException e) {
        e.printStackTrace();
        return null;
    }
}