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.consult.app.dao.impl.UserDaoImpl.java

@Override
public List<Object> getMessages(TypeBeforeAfterRequest req, int mode, long userId) {

    String condition = "", hql = "";
    MessageInterceptor inter = null;/*ww w  . j  a va  2  s .  co m*/
    condition = " and message.updateTime>? and message.updateTime<=? order by message.updateTime desc";
    if (req.getType() == Constant.MESSAGE_TYPE_CARGO) {
        hql = "select new com.consult.app.response.cargo.MyCargoMessageItem"
                + "(message.messageId, case when message.type>=1 and message.type<4 then 1 else 0 end, message.weight, message.capacity,"
                + "message.truckType, message.contact, message.telephone, message.start, message.end,"
                + "message.description, message.updateTime, message.truckLength, message.companyName, message.companyAddress, message.landlines,"
                + "message.picture, message.avatarAuthenticate, message.userId, message.licenseAuthenticate, message.charges, message.cargoType, message.score, message.orderCount, message.messageCount, message.truckLengthSet) "
                + "from CargoMessage message where message.userId=? and message.type>=1 and message.type<=4 ";
        inter = new CargoMessageInterceptor();
    } else if (req.getType() == Constant.MESSAGE_TYPE_FIXED) {
        // Noneed
        return null;
    } else if (req.getType() == Constant.MESSAGE_TYPE_TRUCK) {
        hql = "select new com.consult.app.response.truck.TruckMessageItem"
                + "(message.messageId, message.userId, message.start, message.end, message.travelType,"
                + "profile.userName, message.telephone, message.transfer, "
                + "message.number, message.truckLength, message.truckLoad, message.truckType,"
                + "case when message.type>=" + Constant.TYPE_NORMAL
                + " then 1 else 0 end, message.description, "
                + "message.updateTime, profile.picture, message.commonLines, user.avatarAuthenticate, user.numberAuthenticate) "
                + "from TruckMessage message, Profile profile, User user where user.userId=profile.userId and message.userId=user.userId and message.userId=? ";
        inter = new TruckMessageInterceptor();
    } else {
        return null;
    }
    List<Object> list = new ArrayList<Object>();
    Long triggerTime = req.getAfter();
    if (triggerTime.equals(Long.MAX_VALUE)) {
        triggerTime = System.currentTimeMillis();
    }
    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(hql + condition);
        query.setLong(0, userId);
        query.setLong(1, req.getBefore());
        query.setLong(2, req.getAfter());
        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();
            //               min = metaList.get(metaList.size() - 1).getUpdateTime();
        }
        if (inter.isFinishSearch(req.getBefore())) {
            return list;
        }
        triggerTime = inter.getTriggerTime();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
    //      }
    return list;
}

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

@Override
public List<Object> getLongUnloginUserToken(int appType, Long startId, int count, Long threshTime) {
    try {// w  ww.ja  v a 2s.  co m
        //         String content, Integer pushType, String token,
        //         Integer notificationType, int badgeCount
        String hql = "select new com.consult.app.push.modal.PushQueueItem('" + Constant.LONG_UNLOGIN_MSG_CONTENT
                + "', user.loginFrom, user.token, " + PushUtil.NOTIFICATION_TYPE_BROADCAST + ", 0) "
                + "from User user, Profile profile, Token token "
                + "where user.userId=token.userId and user.token=token.token and user.userId=profile.userId and user.userId>? ";
        if (appType != Cookie.APP_TYPE_ALL) {
            hql = hql + " and user.loginFrom=? ";
        }
        hql = hql + " and profile.userType=? and user.lastLogin<? order by user.userId asc";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        int i = 0;
        query.setLong(i, startId);
        if (appType != Cookie.APP_TYPE_ALL) {
            query.setInteger(++i, appType);
        }
        query.setInteger(++i, Constant.USER_TYPE_TRUCKER);
        query.setLong(++i, threshTime);
        query.setFirstResult(0);
        query.setMaxResults(count + 1);
        return query.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

@Override
public List<Object> getTodayLoginUsers(IdTypeBeforeAfterRequest req) {
    String hql = "";
    try {/*from w  w  w .j  a v  a 2s.c o m*/
        if (req.getId().intValue() <= Constant.CITY_ALL) {
            hql = "select new com.consult.app.response.admin.LastLoginItem(profile.userName, user.telephone, user.lastLogin) "
                    + "from User user, Profile profile "
                    + "where user.userId=profile.userId and user.lastLogin>? and user.lastLogin<? ";
            if (req.getType() == Constant.USER_TYPE_TRUCKER) {
                hql = hql + " and profile.userType=?";
            } else {
                hql = hql + " and profile.userType<>?";
            }
            hql = hql + " order by user.lastLogin desc";
            Query query = sessionFactory.getCurrentSession().createQuery(hql);
            //            query.setLong(0, TimeUtils.getStartOfDay(System.currentTimeMillis()));
            query.setLong(0, req.getBefore());
            query.setLong(1, req.getAfter());
            query.setInteger(2, Constant.USER_TYPE_TRUCKER);
            query.setFirstResult(0);
            query.setMaxResults(req.getCount());
            return query.list();
        } else {
            hql = "select new com.consult.app.response.admin.LastLoginItem(profile.userName, user.telephone, user.lastLogin) "
                    + "from User user, Profile profile "
                    + "where user.userId=profile.userId and user.lastLogin>? and user.lastLogin<? ";
            if (req.getType() == Constant.USER_TYPE_TRUCKER) {
                hql = hql + " and profile.userType=?";
            } else {
                hql = hql + " and profile.userType<>?";
            }
            hql = hql + " and user.city=? order by user.lastLogin desc";
            Query query = sessionFactory.getCurrentSession().createQuery(hql);
            query.setLong(0, req.getBefore());
            query.setLong(1, req.getAfter());
            query.setInteger(2, Constant.USER_TYPE_TRUCKER);
            query.setInteger(3, req.getId().intValue());
            query.setFirstResult(0);
            query.setMaxResults(req.getCount());
            return query.list();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

@Override
public List<Object> getPreAuthenticateUsers(CountTypeRequest req) {
    String hql = "";
    List<Object> list1 = null, list2 = null;
    String cond = "";
    if (req.getType() == 1) {
        cond = " and profile.userType=1 ";
    } else if (req.getType() > 1) {
        cond = " and profile.userType>1 ";
    }//from  w  w  w . j a v a 2 s.c o  m
    try {
        //String userId, int type, String field1,
        //         String field2, Long updateTime, int userType
        hql = "select new com.consult.app.response.admin.AuthenticateUserItem("
                + "user.userId, 1, cer.idCard, profile.picture, profile.companyName, profile.companyAddress, user.updateTime, profile.userType, user.telephone, cer.idNumber) "
                + "from User user, Profile profile, Certificate cer "
                + "where user.userId=profile.userId and user.certificateId=cer.userId "
                + "and user.avatarAuthenticate=? and user.isAuthenticate=? " + cond
                + " order by user.updateTime asc";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setLong(0, Constant.USER_AUTHENTICATE_WAIT);
        query.setLong(1, Constant.USER_AUTHENTICATE_SUCCESS);
        query.setFirstResult(0);
        query.setMaxResults(req.getCount());
        return query.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    //      try {
    //         //String userId, int type, String field1,
    ////         String field2, Long updateTime, int userType
    //         hql = "select new com.consult.app.response.admin.AuthenticateUserItem(" +
    //         "user.userId, 2, profile.drivingLicense, profile.number, profile.companyName, profile.companyAddress, user.updateTime, profile.userType, user.telephone, '') " +
    //         "from User user, Profile profile " +
    //         "where user.userId=profile.userId " +
    //         "and user.numberAuthenticate=? order by user.updateTime asc";
    //         Query query = sessionFactory.getCurrentSession().createQuery(hql);
    //         query.setLong(0, Constant.USER_AUTHENTICATE_WAIT);
    //         query.setFirstResult(0);
    //         query.setMaxResults(req.getCount()); 
    //         list2 = query.list();
    //      } catch (Exception e) {
    //         e.printStackTrace();
    //         return null;
    //      }
    //      return Cookie.mergeSort(new ArrayList(list1), new ArrayList(list2), 20, Cookie.ORDER_ASC);
}

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

@Override
public List<Object> getPreIdAuthenticateUsers(CountRequest req) {
    try {//ww w . j  a  v  a 2 s  .c om
        //String userId, int type, String field1,
        //         String field2, Long updateTime, int userType
        String hql = "select new com.consult.app.response.admin.AuthenticateUserItem("
                + "user.userId, 3, certificate.idNumber, certificate.realName, user.updateTime, profile.userType, user.telephone) "
                + "from User user, Certificate certificate, Profile profile "
                + "where user.userId=profile.userId and user.certificateId=certificate.userId "
                + "and user.isAuthenticate=? order by user.userId asc";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setLong(0, Constant.USER_AUTHENTICATE_WAIT);
        query.setFirstResult(0);
        query.setMaxResults(req.getCount());
        return query.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

@Override
public List<Object> getLongUnloginUserTelephone(int appType, Long startId, int count, Long threshTime) {
    try {/* www .j  a  v  a2 s  .  co  m*/
        //         String content, Integer pushType, String token,
        //         Integer notificationType, int badgeCount
        String hql = "select new com.consult.app.response.user.TelephoneItem(user.telephone) "
                + "from User user, Token token "
                + "where user.userId=token.userId and user.token=token.token and user.userId>? ";
        if (appType != Cookie.APP_TYPE_ALL) {
            hql = hql + " and user.loginFrom=? ";
        }
        hql = hql + " and profile.userType=? and user.lastLogin<? order by user.userId asc";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        int i = 0;
        query.setLong(i, startId);
        if (appType != Cookie.APP_TYPE_ALL) {
            query.setInteger(++i, appType);
        }
        query.setInteger(++i, Constant.USER_TYPE_TRUCKER);
        query.setLong(++i, threshTime);
        query.setFirstResult(0);
        query.setMaxResults(count + 1);
        return query.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

@Override
public List<Object> getReadyToCheckUser(Long startId, int count) {

    try {//from   w w  w.  j ava 2s .c  o  m
        //         String content, Integer pushType, String token,
        //         Integer notificationType, int badgeCount
        String hql = "from User user " + "where user.userId>? and user.smsLocate=? order by user.userId asc";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setLong(0, startId);
        query.setInteger(1, Constant.SMS_LOCATE_READY_TO_CHECK);
        query.setFirstResult(0);
        query.setMaxResults(count + 1);
        return query.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.copyright.common.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Query,./*w  w w  .j  a  v  a 2 s .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:com.cyclopsgroup.tornado.hibernate.HqlLargeList.java

License:CDDL license

/**
 * Overwrite or implement method iterate()
 *
 * @see com.cyclopsgroup.waterview.LargeList#iterate(int, int, com.cyclopsgroup.waterview.LargeList.Sorting[])
 */// ww  w .j ava2s. c o  m
public Iterator iterate(int startPosition, int maxRecords, Sorting[] sortings) throws Exception {
    if (StringUtils.isEmpty(hql)) {
        throw new IllegalStateException("query is still emtpy");
    }
    Session s = hibernate.getSession(dataSource);
    StringBuffer sb = new StringBuffer(hql);

    boolean first = true;
    for (int i = 0; i < sortings.length; i++) {
        Sorting sorting = sortings[i];
        if (first) {
            sb.append(" ORDER BY ");
            first = false;
        } else {
            sb.append(", ");
        }
        sb.append(sorting.getName());
        if (sorting.isDescending()) {
            sb.append(" DESC");
        }
    }

    Query q = s.createQuery(sb.toString());
    HashSet parameterNames = new HashSet();
    CollectionUtils.addAll(parameterNames, q.getNamedParameters());
    for (Iterator i = parameters.values().iterator(); i.hasNext();) {
        Parameter p = (Parameter) i.next();
        if (parameterNames.contains(p.getName())) {
            q.setParameter(p.getName(), p.getValue(), p.getType());
        }
    }
    q.setFirstResult(startPosition);
    if (maxRecords > 0) {
        q.setMaxResults(maxRecords);
    }
    return q.iterate();
}

From source file:com.dungnv.vfw5.base.dao.BaseFWDAOImpl.java

License:Open Source License

public List<T> find(String boName, List<ConditionBean> lstCondition, String order, int start, int maxResult,
        String logic) {//ww  w .j  a  v a2s  .  c om
    //        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.isEmpty()) {
            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;
    }
}