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

public String relookService() {
    Session s = null;/*from   w  w  w  . j  av  a  2s .c om*/
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Query query = s.createQuery("from ServiceInfo where state=0");
        List<ServiceInfo> is = query.list();
        for (ServiceInfo i : is) {
            Vehicle v = (Vehicle) s.get(Vehicle.class, i.getCarframeNum());
            BeanUtils.copyProperties(i, v, new String[] { "state" });
            s.update(v);
            i.setState(1);
            s.update(i);

            Query q_c = s.createQuery(
                    "select c from Contract c where c.state in (2,3) and c.idNum=:idNum and c.carframeNum=:carframeNum ");
            q_c.setString("idNum", v.getDriverId());
            q_c.setString("carframeNum", v.getCarframeNum());
            q_c.setMaxResults(1);
            Contract c = (Contract) q_c.uniqueResult();

            if (c != null) {
                Query q_va = s.createQuery("from VehicleApproval c where c.contractId=:cid and c.checkType=0 ");
                q_va.setInteger("cid", c.getId());
                q_va.setMaxResults(1);
                VehicleApproval approval = (VehicleApproval) q_va.uniqueResult();
                approval.setOperateCardDate(v.getOperateCardTime());
                s.saveOrUpdate(approval);
            }

        }
        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleAction.java

public String relookServiceRight() {
    Session s = null;// w  w  w .ja  v  a  2  s.  co m
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Query query = s.createQuery("from ServiceRightInfo where state=0");
        List<ServiceRightInfo> is = query.list();
        for (ServiceRightInfo i : is) {
            Vehicle v = (Vehicle) s.get(Vehicle.class, i.getCarframeNum());
            BeanUtils.copyProperties(i, v, new String[] { "state" });
            s.update(v);
            i.setState(1);
            s.update(i);

            Query q_c = s.createQuery(
                    "select c from Contract c where c.state in (2,3) and c.idNum=:idNum and c.carframeNum=:carframeNum ");
            q_c.setString("idNum", v.getDriverId());
            q_c.setString("carframeNum", v.getCarframeNum());
            q_c.setMaxResults(1);
            Contract c = (Contract) q_c.uniqueResult();

            if (c != null) {
                Query q_va = s.createQuery("from VehicleApproval c where c.contractId=:cid and c.checkType=0 ");
                q_va.setInteger("cid", c.getId());
                q_va.setMaxResults(1);
                VehicleApproval approval = (VehicleApproval) q_va.uniqueResult();
                approval.setOperateCardDate(v.getOperateCardTime());
                s.saveOrUpdate(approval);
            }

        }
        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleApprovalAction.java

public String vehicleApprovalInterrupt() {
    Session s = null;//  w w  w  .j a v a  2s.c o m
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();

        Message msg = new Message();

        msg.setFromUser(vehicleApproval.getInterruptPerson());
        msg.setTime(new Date());

        String reason = vehicleApproval.getInterruptReason();

        User u = (User) s.get(User.class, vehicleApproval.getInterruptPerson());
        vehicleApproval = (VehicleApproval) s.get(VehicleApproval.class, vehicleApproval.getId());
        Contract c = (Contract) s.get(Contract.class, vehicleApproval.getContractId());
        c.setState((short) -2);
        s.saveOrUpdate(c);

        vehicleApproval.setState(-vehicleApproval.getState());
        vehicleApproval.setInterruptTime(new Date());
        vehicleApproval.setInterruptPerson(u.getUid());
        vehicleApproval.setInterruptReason(reason);
        s.saveOrUpdate(vehicleApproval);

        msg.setCarframeNum(c.getCarframeNum());
        msg.setIdNum(c.getIdNum());

        if (vehicleApproval.getCheckType() == 0) {
            msg.setType("");
            if (StringUtils.isNotBlank(c.getCarNumOld())
                    && (c.getContractFrom() == null || c.getContractFrom() == 0)) {
                Query q_v = s.createQuery("from Vehicle where licenseNum = :carNum ");
                q_v.setString("carNum", c.getCarNumOld().trim());
                q_v.setMaxResults(1);
                Vehicle v = (Vehicle) q_v.uniqueResult();
                if (v != null) {
                    v.setReused(false);
                    s.saveOrUpdate(v);
                }
            }
        } else {
            msg.setType("");
        }

        Driver d = (Driver) s.get(Driver.class, c.getIdNum());

        msg.setMsg(String.format(
                "%tF %s?\n" + "%s(%s)  %s(%s) %s ",
                msg.getTime(), u.getUname(), c.getCarNum(), c.getCarframeNum(), d.getName(), c.getIdNum(),
                reason));

        s.saveOrUpdate(msg);

        Query q_us = s.createQuery(String.format(
                "from RelationUr where rid in (select rid from Role where rname in ('%s','%s','%s','%s','%s','%s','%s') ) ",
                "??", "????", "??", "?", "",
                "??", ""));
        List<RelationUr> users = q_us.list();

        for (RelationUr relationUr : users) {
            MessageToUser mu = new MessageToUser();
            mu.setUid(relationUr.getUid());
            mu.setMid(msg.getId());
            mu.setAlreadyRead(false);
            s.saveOrUpdate(mu);
        }

        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        request.setAttribute("msgStr", "" + e.getMessage());
        return SUCCESS;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    request.setAttribute("msgStr", "??");
    return SUCCESS;
}

From source file:com.dz.module.vehicle.VehicleDaoImpl.java

@SuppressWarnings("unchecked")
@Override/*from  ww w .  ja  v a2  s.c o m*/
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);
        }/* www .  ja va2  s  .  c  o  m*/
    }
    if (begin >= 0 && max > 0) {
        query.setFirstResult(begin);
        query.setMaxResults(max);
    }
    return query.list();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public CrFileNode getBaseByNodeUuid(final String nodeUuid) {
    Query query = getCurrentSesssion().createQuery(GET_BASE_BY_NODE_UUID);
    query.setString("uuid", nodeUuid);
    query.setMaxResults(1);
    List list = query.list();/*from  w w w.j a v  a  2  s  . com*/
    if (list == null || list.size() == 0)
        return null;

    return (CrFileNode) list.get(0);
}

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 ";
    }/*from   ww  w .j a  va2s .com*/
    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);/*  ww w.  j  a  v  a 2  s  .  c om*/
    if (count > 0)
        query.setMaxResults(count);

    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 {//w w  w. ja  v a  2  s.  c  om
        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.HistoryDAOHibernate.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<History> getByUuid(final String uuid, final int startVer, final int returnCount,
        final Date touchedDate) {
    Query query;
    if (touchedDate == null) {
        query = getCurrentSesssion().createQuery(GET_HISTORY_BY_UUID);
        query.setString(0, uuid);/*ww w . j a  v a2 s .c om*/
        query.setInteger(1, startVer <= 0 ? Integer.MAX_VALUE : startVer);
    } else {
        query = getCurrentSesssion().createQuery(GET_HISTORY_BY_UUID_OLDER_DATE);
        query.setString(0, uuid);
        query.setDate(1, touchedDate);
        query.setInteger(2, startVer <= 0 ? Integer.MAX_VALUE : startVer);
    }
    if (returnCount > 0) {
        query.setMaxResults(returnCount);
    }
    return query.list();

}