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.dz.module.vehicle.VehicleDaoImpl.java

@SuppressWarnings("unchecked")
@Override// w ww  .  ja  v a  2s.c om
public List<Vehicle> vehicleSearch(Page page, Vehicle vehicle, Triplet<String, String, Object>... conditions)
        throws HibernateException {
    // TODO Auto-generated method stub
    List<Vehicle> l = new ArrayList<Vehicle>();
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();

        String sql = "from Vehicle where state!=-1 ";
        if (vehicle != null) {

            if (!StringUtils.isEmpty(vehicle.getCarframeNum())) {
                sql += "and carframeNum like :carframeNum ";
            }

            if (!StringUtils.isEmpty(vehicle.getEngineNum())) {
                sql += "and engineNum like :engineNum ";
            }

            if (!StringUtils.isEmpty(vehicle.getCarMode())) {
                sql += "and carMode like :carMode ";
            }

            if (!StringUtils.isEmpty(vehicle.getCertifyNum())) {
                sql += "and certifyNum like :certifyNum ";
            }

            if (!StringUtils.isEmpty(vehicle.getDept())) {
                sql += "and dept like :dept ";
            }

            if (!StringUtils.isEmpty(vehicle.getDriverId())) {
                sql += "and driverId like :driverId ";
            }

            if (!StringUtils.isEmpty(vehicle.getLicenseNum())) {
                sql += "and licenseNum like :licenseNum ";
            }

            if (vehicle.getState() != null) {
                sql += "and state=:state ";
            }
        }

        for (Triplet<String, String, Object> condition : conditions) {
            if (condition != null) {
                sql += String.format("and %s %s :%s ", condition.getValue0(), condition.getValue1(),
                        condition.getValue0());
            }
        }

        sql += " order by licenseNum ";

        Query query = session.createQuery(sql);
        query.setMaxResults(page.getEveryPage());
        query.setFirstResult(page.getBeginIndex());

        if (vehicle != null) {
            if (!StringUtils.isEmpty(vehicle.getCarframeNum())) {
                query.setString("carframeNum", "%" + vehicle.getCarframeNum() + "%");
            }

            if (!StringUtils.isEmpty(vehicle.getEngineNum())) {
                query.setString("engineNum", "%" + vehicle.getEngineNum() + "%");
            }

            if (!StringUtils.isEmpty(vehicle.getCarMode())) {
                query.setString("carMode", "%" + vehicle.getCarMode() + "%");
            }

            if (!StringUtils.isEmpty(vehicle.getCertifyNum())) {
                query.setString("certifyNum", "%" + vehicle.getCertifyNum() + "%");
            }

            if (!StringUtils.isEmpty(vehicle.getDept())) {
                query.setString("dept", "%" + vehicle.getDept() + "%");
            }

            if (!StringUtils.isEmpty(vehicle.getDriverId())) {
                query.setString("driverId", "%" + vehicle.getDriverId() + "%");
            }

            if (!StringUtils.isEmpty(vehicle.getLicenseNum())) {
                query.setString("licenseNum", "%" + vehicle.getLicenseNum() + "%");
            }

            if (vehicle.getState() != null) {
                query.setInteger("state", vehicle.getState());
            }
        }

        for (Triplet<String, String, Object> condition : conditions) {
            if (condition != null) {
                query.setParameter(condition.getValue0(), condition.getValue2());
            }
        }

        l = query.list();
        query = null;
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return l;
}

From source file:com.easyjf.web.tools.HibernateDaoSurport.java

License:Apache License

public List query(Class clz, String scope, Collection paras, int begin, int max) {
    Query query = getCurrentSession().createQuery("from " + clz + " where " + scope);
    int parameterIndex = 0;
    if (paras != null && paras.size() > 0) {
        for (Object obj : paras) {
            query.setParameter(parameterIndex++, obj);
        }/*from  ww w.ja  va 2 s .co m*/
    }
    if (begin >= 0 && max > 0) {
        query.setFirstResult(begin);
        query.setMaxResults(max);
    }
    return query.list();
}

From source file:com.edgenius.core.dao.hibernate.UserDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
//JDK1.6 @Override
public List<User> getUsers(final int start, final int returnSize, String sortBy, final String filter,
        boolean sortByDesc) {
    String orderBy = "";
    String filterWith = "";

    if (!StringUtils.isBlank(filter)) {
        filterWith = " where u.username like :filter or u.fullname like :filter or u.contact.email like :filter ";
    }// w w w  . j  a v  a  2 s  . c o m
    if (!StringUtils.isBlank(sortBy)) {
        StringBuffer orderSb = new StringBuffer(" order by ");
        String[] sortStr = sortBy.split("\\|");
        String seq = (sortByDesc ? " desc" : " asc");
        for (String str : sortStr) {
            int sort = NumberUtils.toInt(str, -1);
            if (sort == -1)
                continue;
            if (sort == User.SORT_BY_CREATED_DATE)
                orderSb.append("u.createdDate ").append(seq).append(",");
            else if (sort == User.SORT_BY_EMAIL)
                orderSb.append("u.contact.email ").append(seq).append(",");
            else if (sort == User.SORT_BY_USERNAME)
                orderSb.append("u.username ").append(seq).append(",");
            else if (sort == User.SORT_BY_FULL_NAME)
                orderSb.append("u.fullname ").append(seq).append(",");
        }
        if (orderSb.length() > 0) {
            orderBy = orderSb.toString();
            if (orderBy.endsWith(",")) {
                //remove last ","
                orderBy = orderBy.substring(0, orderBy.length() - 1);
            }
        }
    } else {
        //default order
        orderBy = " order by u.createdDate desc";
    }

    final String sql = SQL_GET_USERS + filterWith + orderBy;
    Query query = getCurrentSesssion().createQuery(sql);
    if (!StringUtils.isBlank(filter)) {
        query.setString("filter", "%" + filter.trim() + "%");
    }
    query.setFirstResult(start);
    if (returnSize > 0)
        query.setMaxResults(returnSize);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.ActivityLogDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<ActivityLog> getByCount(final int start, final int count) {
    Query query = getCurrentSesssion().createQuery(GET_BY_COUNT);
    if (start > 0)
        query.setFirstResult(start);
    if (count > 0)
        query.setMaxResults(count);//  w ww  .j ava2 s.  co m

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.ActivityLogDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<ActivityLog> getByUser(final int start, final int count, final User user) {

    Query query;
    if (user == null || user.isAnonymous()) {
        query = getCurrentSesssion().createQuery(GET_BY_ANONYMOUS_COUNT);
    } else {/*from  www  . j a  va 2s .c  o  m*/
        query = getCurrentSesssion().createQuery(GET_BY_USER_COUNT);
        query.setInteger("user", user.getUid());
    }
    if (start > 0)
        query.setFirstResult(start);
    if (count > 0)
        query.setMaxResults(count);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.NotificationDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Notification> getResourceMessages(final User user, final boolean sysAdmin,
        final List<Resource> spaceResList, final List<Resource> adminResList, final int start,
        final int retCount) {
    StringBuffer sql = new StringBuffer(GET_MESSAGES_PREFIX);

    Map<Integer, String> queryParams = new HashMap<Integer, String>();
    int queryPos = 0;
    //if users is instance admin, then get all messages... is it good????
    if (!sysAdmin) {
        //first, get messages to all users and specified to given user
        sql.append(" where n.targetType=").append(SharedConstants.MSG_TARGET_ALL_USERS).append(" or ");
        sql.append(" (n.targetType=").append(SharedConstants.MSG_TARGET_USER).append(" and n.targetName=:p")
                .append(queryPos).append(") ");
        queryParams.put(queryPos++, user.getUsername());

        if (adminResList != null && adminResList.size() > 0) {
            sql.append(" or ");

            for (int idx = 0; idx < adminResList.size(); idx++) {
                Resource resource = adminResList.get(idx);
                if (SecurityValues.RESOURCE_TYPES.SPACE.equals(resource.getType())) {
                    //spaces admin permissions
                    sql.append(" (n.targetType=").append(SharedConstants.MSG_TARGET_SPACE_ADMIN_ONLY)
                            .append(" and ");
                    sql.append(" n.targetName=:p").append(queryPos + idx).append(") or ");
                    queryParams.put(queryPos + idx, resource.getResource());
                }/*from   w ww  .ja  v  a  2 s  .c  om*/
            }
            //remove last "or"
            sql.delete(sql.length() - 3, sql.length());
        }

        if (spaceResList != null && spaceResList.size() > 0) {
            sql.append(" or ");
            queryPos = queryParams.size();
            for (int idx = 0; idx < spaceResList.size(); idx++) {
                Resource resource = spaceResList.get(idx);
                if (SecurityValues.RESOURCE_TYPES.SPACE.equals(resource.getType())) {
                    //spaces all users
                    sql.append(" (n.targetType=").append(SharedConstants.MSG_TARGET_SPACE_CONTRIBUTE_USERS)
                            .append(" and ");
                    sql.append(" n.targetName=:p").append(queryPos + idx).append(") or ");
                    queryParams.put(queryPos + idx, resource.getResource());
                }
            }
            //remove last "or"
            sql.delete(sql.length() - 3, sql.length());
        }

    }

    sql.append(GET_MESSAGES_SUFFIX);
    Query query = getCurrentSesssion().createQuery(sql.toString());
    for (Entry<Integer, String> entry : queryParams.entrySet()) {
        query.setString("p" + entry.getKey(), entry.getValue());
    }

    if (start > 0)
        query.setFirstResult(start);
    if (retCount > 0)
        query.setMaxResults(retCount);

    return query.list();
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getRecentPages(final String spaceUname, final int start, final int count,
        final boolean sortByModify) {
    if (StringUtils.isBlank(spaceUname)) {
        String sql;//from  ww  w .ja  v  a  2 s . c o m
        if (!sortByModify) {
            sql = GET_INSTANCE_CURRENT_PAGES + "order by p.createdDate  desc";
        } else {
            sql = GET_INSTANCE_CURRENT_PAGES + "order by p.modifiedDate desc";
        }
        Query query = getCurrentSesssion().createQuery(sql);
        if (start > 0)
            query.setFirstResult(start);
        return query.setMaxResults(count).list();
    } else {
        String sql;
        if (!sortByModify) {
            sql = GET_SPACE_CURRNET_PAGES + "order by p.createdDate  desc";
        } else {
            sql = GET_SPACE_CURRNET_PAGES + "order by p.modifiedDate desc";
        }
        Query query = getCurrentSesssion().createQuery(sql);
        query.setString("spaceUname", spaceUname);
        if (start > 0)
            query.setFirstResult(start);
        return query.setMaxResults(count).list();
    }
}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getPageForSitemap(Date lastModifiedDate, final int start, final int returnNum) {
    Query query = getCurrentSesssion().createQuery(GET_FOR_SITE_MAP);
    query.setParameter("lastModifiedDate", lastModifiedDate);

    if (start > 0)
        query.setFirstResult(start);

    List<Object[]> list;
    if (returnNum > 0) {
        list = query.setMaxResults(returnNum).list();
    } else {//from  w w  w .  j av  a2 s. c o  m
        list = query.list();
    }
    if (list == null) {
        return null;
    }

    List<Page> pageList = new ArrayList<Page>(list.size());
    for (Object[] objects : list) {
        Page page = new Page();
        int idx = 0;
        //pageUuid, title, spaceUid, and content
        page.setUid((Integer) objects[idx++]);
        page.setPageUuid((String) objects[idx++]);
        page.setTitle((String) objects[idx++]);
        page.setModifiedDate((Date) objects[idx++]);

        Space space = new Space();
        space.setUnixName((String) objects[idx++]);
        page.setSpace(space);

        pageList.add(page);
    }
    return pageList;

}

From source file:com.edgenius.wiki.dao.hibernate.PageDAOHibernate.java

License:Open Source License

public List<Page> getPageForIndexing(final int start, final int returnNum) {
    Query query = getCurrentSesssion().createQuery(GET_PAGE_FOR_INDEXING);
    if (start > 0)
        query.setFirstResult(start);

    List<Object[]> list;
    if (returnNum > 0) {
        list = query.setMaxResults(returnNum).list();
    } else {/* www.  j  a  va  2  s  .  c o  m*/
        list = query.list();
    }

    if (list == null) {
        return null;
    }

    List<Page> pageList = new ArrayList<Page>(list.size());
    for (Object[] objects : list) {
        Page page = new Page();
        int idx = 0;
        //pageUuid, title, spaceUid, and content
        page.setPageUuid((String) objects[idx++]);
        page.setTitle((String) objects[idx++]);
        page.setModifiedDate((Date) objects[idx++]);

        Space space = new Space();
        space.setUid((Integer) objects[idx++]);

        PageContent content = new PageContent();
        content.setContent((String) objects[idx++]);

        page.setSpace(space);
        page.setContent(content);

        pageList.add(page);
    }
    return pageList;

}

From source file:com.edgenius.wiki.dao.hibernate.SpaceDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Space> getSpaces(final int start, final int returnSize, final String sortBy, final String filter,
        final boolean sortByDesc) {
    String orderBy = "";
    String filterWith = "";
    if (!StringUtils.isBlank(filter)) {
        filterWith = "where s.name like :filter or s.unixName like :filter or s.description like :filter ";
    }//from  w  w w  .  ja va2s.  c  o  m
    if (!StringUtils.isBlank(sortBy)) {
        StringBuffer orderSb = new StringBuffer(" order by ");
        String[] sortStr = sortBy.split("\\|");
        String seq = (sortByDesc ? " desc" : " asc");
        for (String str : sortStr) {
            int sort = NumberUtils.toInt(str, -1);
            if (sort == -1)
                continue;
            if (sort == Space.SORT_BY_PAGE_SCORE)
                orderSb.append("s.score ").append(seq).append(",");
            else if (sort == Space.SORT_BY_CREATEON)
                orderSb.append("s.createdDate ").append(seq).append(",");
            else if (sort == Space.SORT_BY_CREATEBY)
                orderSb.append("s.creator ").append(seq).append(",");
            else if (sort == Space.SORT_BY_SPACE_TITLE)
                orderSb.append("s.name ").append(seq).append(",");
            else if (sort == Space.SORT_BY_SPACEKEY)
                orderSb.append("s.unixName ").append(seq).append(",");
        }
        if (orderSb.length() > 0) {
            orderBy = orderSb.toString();
            if (orderBy.endsWith(",")) {
                //remove last ","
                orderBy = orderBy.substring(0, orderBy.length() - 1);
            }
        }
    } else {
        //default order
        orderBy = " order by s.score desc, s.createdDate desc";
    }

    Query query = getCurrentSesssion().createQuery(GET_SPACES + filterWith + orderBy);
    if (!StringUtils.isBlank(filter)) {
        query.setString("filter", "%" + filter.trim() + "%");
    }
    query.setFirstResult(start);
    if (returnSize > 0)
        query.setMaxResults(returnSize);

    return query.list();
}