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.catexpress.dao.impl.GenericDaoImpl.java

/**
 *
 * @param jpql/*from w  w w.  j a va  2 s  .c o  m*/
 * @param mapa
 * @param maximoResultados
 * @return
 */
public List<T> executeQuery(String jpql, Map<String, Object> mapa, Integer maximoResultados) {
    List<T> l = null;
    try {
        s = HibernateUtil.getSessionFactory().getCurrentSession();
        t = s.beginTransaction();
        Query q = s.createQuery(jpql);
        for (Map.Entry<String, Object> entry : mapa.entrySet()) {
            q.setParameter(entry.getKey(), entry.getValue());
        }
        if (maximoResultados == null) {
            l = (List<T>) q.list();
        } else {
            l = (List<T>) q.setMaxResults(maximoResultados).list();
        }
        t.commit();
    } catch (Exception e) {
        t.rollback();
        throw e;
    }
    return l;
}

From source file:com.catexpress.dao.impl.GenericDaoImpl.java

public List<T> executeQuery(int max, String jpql, Object... values) {
    List<T> l = null;//  w  w w . ja  va  2 s  .c o  m
    try {
        s = HibernateUtil.getSessionFactory().getCurrentSession();
        t = s.beginTransaction();
        Query q = s.createQuery(jpql);
        int i = 1;
        for (Object object : values) {
            q.setParameter(i, object);
            i++;
        }
        q.setMaxResults(max);
        l = (List<T>) q.list();
        t.commit();
    } catch (Exception e) {
        t.rollback();
        throw e;
    }
    return l;
}

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);//  w  w w .  jav a2 s  . co  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 T queryEntity(String hql, Object[] params) {
    Session session = PersistContext.getSession(isCloudStackSession);
    Query query = session.createQuery(hql);
    query.setMaxResults(1);
    QueryHelper.bindParameters(query, params);
    return (T) query.uniqueResult();
}

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);/* w ww . jav  a2 s  . c o m*/
    query.setMaxResults(limit);
    return (List<T>) query.list();
}

From source file:com.cms.utils.BaseFWDAOImpl.java

License:Open Source License

public List<T> find(String boName, List<ConditionBean> lstCondition, String order, int start, int maxResult,
        String logic) {//  w w w . j a  v a2  s.  c  o  m
    //        if (logic == null) {
    //            logic = ParamUtils.LOGIC_AND;
    //        }
    try {
        StringBuilder sql = new StringBuilder();
        sql.append(" from ");
        sql.append(boName);
        sql.append(" where 1=1 ");
        if (lstCondition != null && lstCondition.size() > 0) {
            buildConditionQuery(sql, lstCondition);
        }
        if (order != null && !order.equals("")) {
            sql.append(" order by ");
            sql.append(order);
        }
        Query query = getSession().createQuery(sql.toString());
        if (maxResult != 0) {
            query.setFirstResult(start);
            query.setMaxResults(maxResult);
        }
        fillConditionQuery(query, lstCondition);
        return query.list();
    } catch (HibernateException he) {
        log.error(he.getMessage(), he);
        return null;
    }
}

From source file:com.cms.utils.BaseFWDAOImpl.java

License:Open Source License

public List<T> findSession(String boName, List<ConditionBean> lstCondition, String order, int start,
        int maxResult, String logic, Session session) {
    //        if (logic == null) {
    //            logic = ParamUtils.LOGIC_AND;
    //        }/*from   ww  w . j  a v a2  s .  c om*/
    try {
        StringBuilder sql = new StringBuilder();
        sql.append(" from ");
        sql.append(boName);
        sql.append(" where 1=1 ");
        if (lstCondition != null && lstCondition.size() > 0) {
            buildConditionQuery(sql, lstCondition);
        }
        if (order != null && !order.equals("")) {
            sql.append(" order by ");
            sql.append(order);
        }
        Query query = session.createQuery(sql.toString());
        if (maxResult != 0) {
            query.setFirstResult(start);
            query.setMaxResults(maxResult);
        }
        fillConditionQuery(query, lstCondition);
        return query.list();
    } catch (HibernateException he) {
        log.error(he.getMessage(), he);
        return null;
    }
}

From source file:com.connexience.server.model.datasets.DatasetsUtils.java

License:Open Source License

/** Get values for a Json multiple value object */
public static JSONContainer getJsonMultipleValueData(Session session, JsonMultipleValueItem item,
        int startIndex, int maxResults, String[] filterKeys) throws ConnexienceException {
    try {//from w w  w.  j a v a2s  .  c  o m
        Query q = session.createQuery("from JsonDataRow as obj where obj.itemId=:itemid order by obj.id asc");
        q.setLong("itemid", item.getId());

        if (startIndex >= 0) {
            q.setFirstResult(startIndex);
        }

        if (maxResults >= 0) {
            q.setMaxResults(maxResults);
        }

        List rows = q.list();
        return createResultFromList(rows, filterKeys);
    } catch (Exception ex) {
        throw new ConnexienceException("Error getting JSON values: " + ex.getMessage());
    }
}

From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java

/**
 * Common Search cargo, used by 274 Version!
 * This supports multiple truck length/*w w  w.  j  a v a  2s  .c o m*/
 * 
 */
private List<Object> getMultipleTruckLengthSearch(SearchCargoRequest req, City endCity) {

    Long today = TimeUtils.getStartOfDay(System.currentTimeMillis());
    StringBuilder sb = new StringBuilder(String.format(Constant.CARGO_SEARCH_MULTIPLE_TRUCKLENGTH, today));

    if (Double.valueOf(req.getTruckLength()) >= 0) {
        sb.append(" and find_in_set(").append(req.getTruckLength()).append(",message.truck_length_set)");
    }

    if (req.getTruckType() >= 0) {
        sb.append(" and message.truck_type=").append(req.getTruckType());
    }

    if (req.getWeightRange() > 0) {
        sb.append(" and message.cargo_weight_range=").append(req.getWeightRange());
    }

    sb.append(" and (message.start=").append(req.getStart()).append(" or message.start_father=")
            .append(req.getStart()).append(" or message.start_grand=").append(req.getStart())
            .append(") and (message.end=").append(endCity.getId()).append(" or message.end_father=")
            .append(endCity.getId()).append(" or message.end_grand=").append(endCity.getId()).append(" or 0=")
            .append(endCity.getId()).append(")");

    sb.append(" and message.type>=? and message.type<4 order by message.update_time desc");

    Long triggerTime = req.getAfter();
    if (triggerTime.equals(Long.MAX_VALUE)) {
        triggerTime = System.currentTimeMillis();
    }
    CargoMessageInterceptor inter = new CargoMessageInterceptor();
    int count = req.getCount();
    //      for(int i = 0; i < Constant.SEARCH_TABLE_COUNT; i++) {
    Session session = null;
    try {
        //            Long messageId, int type,
        //            double weight, double capacity, int truckType,
        //            String contact, String telephone, int start, int end, 
        //            String description, long updateTime, double truckLength,
        //            String companyName, String companyAddress, String landlines, 
        //            String picture, int avatarAuthenticate, Long userId
        inter.setShardCriteria(System.currentTimeMillis());
        session = sessionFactory.openSession(inter);
        Query query = session.createSQLQuery(sb.toString()).addScalar("messageId", StandardBasicTypes.LONG)
                .addScalar("type", StandardBasicTypes.INTEGER).addScalar("weight", StandardBasicTypes.DOUBLE)
                .addScalar("capacity", StandardBasicTypes.DOUBLE)
                .addScalar("truckType", StandardBasicTypes.INTEGER)
                .addScalar("contact", StandardBasicTypes.STRING)
                .addScalar("telephone", StandardBasicTypes.STRING)
                .addScalar("start", StandardBasicTypes.INTEGER).addScalar("end", StandardBasicTypes.INTEGER)
                .addScalar("description", StandardBasicTypes.STRING)
                .addScalar("updateTime", StandardBasicTypes.LONG)
                .addScalar("truckLength", StandardBasicTypes.DOUBLE)
                .addScalar("companyName", StandardBasicTypes.STRING)
                .addScalar("companyAddress", StandardBasicTypes.STRING)
                .addScalar("landlines", StandardBasicTypes.STRING)
                .addScalar("picture", StandardBasicTypes.STRING)
                .addScalar("avatarAuthenticate", StandardBasicTypes.INTEGER)
                .addScalar("userId", StandardBasicTypes.LONG)
                .addScalar("licenseAuthenticate", StandardBasicTypes.INTEGER)
                .addScalar("cargoType", StandardBasicTypes.INTEGER)
                .addScalar("score", StandardBasicTypes.DOUBLE)
                .addScalar("orderCount", StandardBasicTypes.INTEGER)
                .addScalar("charges", StandardBasicTypes.INTEGER)
                .addScalar("messageCount", StandardBasicTypes.LONG)
                .addScalar("truckLengthSet", StandardBasicTypes.STRING).setResultTransformer(
                        Transformers.aliasToBean(com.consult.app.response.cargo.CargoMessageItem.class));
        query.setLong(0, req.getBefore());
        query.setLong(1, triggerTime);
        query.setInteger(2, Constant.TYPE_NORMAL);
        query.setFirstResult(0);
        query.setMaxResults(count);
        @SuppressWarnings("unchecked")
        List<Object> tmpList = query.list();
        //            if(tmpList != null && tmpList.size() > 0) {
        //               list.addAll(tmpList);
        //               if(tmpList.size() >= count) {
        //                  tx.commit();
        //                  return list;
        //               }
        //               count -= tmpList.size();
        ////               min = metaList.get(metaList.size() - 1).getUpdateTime();
        //            }
        //            if(inter.isFinishSearch(req.getBefore())) {
        //               tx.commit();
        //               return list;
        //            }
        //            triggerTime = inter.getTriggerTime();
        return tmpList;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
}

From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java

/**
 * Common Search cargo, used by First Version!
 * Deprecated!/* w ww . j  av  a2s .  c om*/
 */
private List<Object> getAllMessage(SearchCargoRequest req, City endCity) {

    StringBuilder sb = new StringBuilder(Constant.CARGO_SEARCH);

    if (req.getTruckLengthRange() > 0) {
        sb.append(" and message.truckLengthRange=").append(req.getTruckLengthRange());
    }

    if (req.getTruckType() >= 0) {
        sb.append(" and message.truckType=").append(req.getTruckType());
    }

    if (req.getWeightRange() > 0) {
        sb.append(" and message.cargoWeightRange=").append(req.getWeightRange());
    }

    sb.append(" and (message.start=").append(req.getStart()).append(" or message.startFather=")
            .append(req.getStart()).append(") and (message.end=").append(endCity.getId())
            .append(" or message.endFather=").append(endCity.getId()).append(" or message.endGrand=")
            .append(endCity.getId()).append(" or 0=").append(endCity.getId()).append(")");

    sb.append(" and message.type>=? and message.type<4 order by message.updateTime desc");

    List<Object> list = new ArrayList<Object>();
    Long triggerTime = req.getAfter();
    if (triggerTime.equals(Long.MAX_VALUE)) {
        triggerTime = System.currentTimeMillis();
    }
    CargoMessageInterceptor inter = new CargoMessageInterceptor();

    int count = req.getCount();
    //      for(int i = 0; i < Constant.SEARCH_TABLE_COUNT; i++) {
    Session session = null;
    try {
        inter.setShardCriteria(triggerTime);
        session = sessionFactory.openSession(inter);
        Query query = session.createQuery(sb.toString());
        query.setLong(0, req.getBefore());
        query.setLong(1, triggerTime);
        query.setInteger(2, Constant.TYPE_NORMAL);
        query.setFirstResult(0);
        query.setMaxResults(count);
        @SuppressWarnings("unchecked")
        List<Object> tmpList = query.list();
        if (tmpList != null && tmpList.size() > 0) {
            list.addAll(tmpList);
            if (tmpList.size() >= count) {
                return list;
            }
            count -= tmpList.size();
        }
        if (inter.isFinishSearch(req.getBefore())) {
            return list;
        }
        triggerTime = inter.getTriggerTime();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
    //      }
    return list;
}