Example usage for org.hibernate Query setFirstResult

List of usage examples for org.hibernate Query setFirstResult

Introduction

In this page you can find the example usage for org.hibernate Query setFirstResult.

Prototype

@Override
    Query<R> setFirstResult(int startPosition);

Source Link

Usage

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();
    }/*  ww w.  j  a  v  a  2s  . c o  m*/

    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));
    }//from   ww w  .jav a 2 s .  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 2  s. c  om*/
 */
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 /* w  w w  .  j a va  2s  .c om*/
 * 
 */
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 ww  w.  ja v a2 s  .  c om
@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  ww.j a v  a  2s  .c  o  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  a  va2 s  .  c  o  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/*from   w ww . j  av  a  2 s .  co  m*/
 * @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;
    }
}

From source file:com.clican.pluto.cms.dao.hibernate.TemplateDaoHibernateImpl.java

License:LGPL

@SuppressWarnings("unchecked")
public List<ITemplateModelSiteRelation> getTemplateModelSiteRelations(ModelDescription modelDescription,
        final String pathExpression, final int firstResult, final int maxResults) {
    String modelName = modelDescription.getFirstCharUpperName();
    final StringBuffer sql = new StringBuffer();
    sql.append("select r from ");
    sql.append(modelName);/*from   ww w .  java 2  s .  c  o m*/
    sql.append(" m,");
    sql.append("Template t,");
    sql.append("Template");
    sql.append(modelName);
    sql.append(
            "SiteRelation r where r.template=t and r.dataModel=m and m.parent.path like :pathExpression order by m.parent.path,m.name");
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery(sql.toString());
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
            query.setParameter("pathExpression", pathExpression);
            return query.list();
        }
    });
}

From source file:com.cloud.bridge.persist.EntityDao.java

License:Open Source License

public List<T> queryEntities(String hql, int offset, int limit, Object[] params) {
    Session session = PersistContext.getSession(isCloudStackSession);
    Query query = session.createQuery(hql);
    QueryHelper.bindParameters(query, params);
    query.setFirstResult(offset);
    query.setMaxResults(limit);//  w  ww . j  a  v a 2s .  c o  m
    return (List<T>) query.list();
}