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.contract.ContractDaoImpl.java

/**
 * /  ???,/*from ww  w.ja  v a  2 s.co m*/
 * @param currentClearTime 
 * @param dept 
 * @param page ,null,???.
  * @return ??.
  */
@Override
public List<Contract> contractSearchAllAvilable(Date currentClearTime, String dept, Page page) {
    List<Contract> l = new ArrayList<Contract>();
    Session session = null;
    Transaction tx = null;
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();
        Query query = null;
        if ("".equals(dept)) {
            query = session.createQuery("from Contract where state in (0,-1,1,4)");
        } else {
            query = session.createQuery("from Contract where state in (0,-1,1,4) and branchFirm = :dept");
            query.setString("dept", dept);
        }
        if (page != null) {
            query.setFirstResult(page.getBeginIndex());
            query.setMaxResults(page.getEveryPage());
        }
        l = query.list();
        Iterator<Contract> iterators = l.iterator();
        filterContractThatStateEq1Charge(currentClearTime, iterators);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return l;
}

From source file:com.dz.module.contract.ContractDaoImpl.java

@SuppressWarnings("unchecked")
@Override/*from w w  w  .ja  v a  2s . c  om*/
public List<Contract> contractSearchAbandoned(Page page) throws HibernateException {
    List<Contract> l = new ArrayList<Contract>();
    Session session = null;
    Transaction tx = null;
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();
        Query query = session.createQuery("from Contract where state not in (0,-1)");
        query.setMaxResults(15);
        query.setFirstResult(page.getBeginIndex());
        l = query.list();
        query = null;
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return l;
}

From source file:com.dz.module.contract.ContractDaoImpl.java

@SuppressWarnings("unchecked")
@Override//from   w  w w. j a  v  a  2 s.c  om
public List<Contract> contractSearchCondition(Page page, Integer contractid, String contractor, Long rent,
        Boolean isabandoned, String sort, String desc) throws HibernateException {
    List<Contract> l = new ArrayList<Contract>();
    Session session = null;
    Transaction tx = null;
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();
        String sql = "from Contract";
        if (contractid == null && StringUtils.isEmpty(contractor) && rent == null) {
            sql += " where 1=1";
        } else {
            sql += " where 0=1";
        }
        if (contractid != null) {
            sql += " or contractId=" + contractid;
        }
        if (!StringUtils.isEmpty(contractor)) {
            sql += " or contractorName=" + contractor;
        }
        if (rent != null) {
            sql += " or rent=" + rent;
        }
        sql += " and isAbandoned=: isabandoned";
        if (sort.equals("0")) {
            sql += " order by contractId";
        } else if (sort.equals("1")) {
            sql += " order by rent";
        }
        if (desc.equals("0")) {
        } else if (desc.equals("1")) {
            sql += " desc";
        }
        System.out.println(sql);
        Query query = session.createQuery(sql);
        query.setBoolean("isabandoned", isabandoned);
        query.setMaxResults(15);
        query.setFirstResult(page.getBeginIndex());
        l = query.list();
        query = null;
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return l;
}

From source file:com.dz.module.contract.ContractDaoImpl.java

@Override
public Contract selectByCarId(String id) {
    Contract c = null;/*  w w  w.j a  v  a2 s . c  o  m*/
    Session session = HibernateSessionFactory.getSession();
    Query query = session.createQuery("from Contract where carframeNum = :id and state=0");
    query.setMaxResults(1);
    query.setString("id", id);
    c = (Contract) query.uniqueResult();
    HibernateSessionFactory.closeSession();
    return c;
}

From source file:com.dz.module.contract.ContractDaoImpl.java

@Override
public Contract selectByCarId(String id, Date d) {
    Contract c = null;// w w w  .j av a 2 s.co  m
    try {
        Session session = HibernateSessionFactory.getSession();
        //         Query query = session.createQuery("from Contract where carframeNum = :id and state=0");
        Query query = session.createQuery(
                "from Contract where id=( select max(id) from Contract where carframeNum = :id and state!=3 and state!=2 and state>=0 ) ");
        query.setString("id", id);
        query.setMaxResults(1);
        c = (Contract) query.uniqueResult();

        Calendar dt = Calendar.getInstance();
        if (c != null && c.getContractBeginDate() != null) {
            dt.setTime(c.getContractBeginDate());
        } else {
            return c;
        }

        if (dt.get(Calendar.DATE) > 26) {

            dt.set(Calendar.DATE, 26);
        } else {
            dt.add(Calendar.MONTH, -1);
            dt.set(Calendar.DATE, 26);
        }

        //         System.out.println("ContractDaoImpl.selectByCarId(),"+dt.toString());

        if (dt.getTime().before(d)) {
            return c;
        } else {
            while (c.getContractFrom() != null) {
                Contract last = (Contract) session.get(Contract.class, c.getContractFrom());
                if (last != null)
                    c = last;
                else
                    return c;

                dt.setTime(c.getContractBeginDate());
                if (dt.get(Calendar.DATE) > 26) {
                    dt.add(Calendar.MONTH, 1);
                    dt.set(Calendar.DATE, 26);
                } else {
                    dt.set(Calendar.DATE, 26);
                }

                if (dt.getTime().before(d))
                    return c;
            }
        }
    } finally {
        HibernateSessionFactory.closeSession();
    }

    return c;
}

From source file:com.dz.module.contract.ContractDaoImpl.java

@SuppressWarnings("unchecked")
@Override/*  ww w. java 2 s .  c  o m*/
public List<Contract> selectAllByStates(Page page, Contract contract, Vehicle vehicle, Driver driver,
        Date beginDate, Date endDate, Short[] states) {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();

        String hql = "from Contract c where c.state in (:states)";

        if (beginDate != null) {
            hql += " and c.contractBeginDate >= :beginDate";
        }

        if (endDate != null) {
            hql += " and c.contractBeginDate <= :endDate";
        }

        if (contract != null) {
            if (!StringUtils.isEmpty(contract.getIdNum())) {
                hql += " and c.idNum like :idNum";
            }

            if (!StringUtils.isEmpty(contract.getCarNum())) {
                hql += " and c.carNum like :carNum";
            }
        }

        Query query = session.createQuery(hql);

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }

        if (endDate != null) {
            query.setDate("endDate", endDate);
        }

        if (contract != null) {
            if (!StringUtils.isEmpty(contract.getIdNum())) {
                query.setString("idNum", "%" + contract.getIdNum() + "%");
            }

            if (!StringUtils.isEmpty(contract.getCarNum())) {
                query.setString("carNum", "%" + contract.getCarNum() + "%");
            }
        }
        query.setParameterList("states", states);
        if (page != null) {
            query.setMaxResults(page.getEveryPage());
            query.setFirstResult(page.getBeginIndex());
        }
        return query.list();
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

From source file:com.dz.module.driver.complain.ComplainDaoImpl.java

@SuppressWarnings("unchecked")
@Override//w w  w  . j a  v a 2  s .c  o m
public List<Complain> selectAll(Page page, Date beginDate, Date endDate) throws HibernateException {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();
        String hql = "from Complain c where 1=1";

        if (beginDate != null) {
            hql += " and c.complainTime >= :beginDate";
        }

        if (endDate != null) {
            hql += " and c.complainTime <= :endDate";
        }

        Query query = session.createQuery(hql);

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }

        if (endDate != null) {
            query.setDate("endDate", endDate);
        }

        query.setMaxResults(page.getEveryPage());
        query.setFirstResult(page.getBeginIndex());
        return query.list();
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

From source file:com.dz.module.driver.complain.ComplainDaoImpl.java

/**
 * ?//from   w w  w .j a  v a2  s. c om
 * @return
 * @throws HibernateException
 */
@SuppressWarnings("unchecked")
@Override
public List<Complain> selectAllByState(Page page, Date beginDate, Date endDate, int state)
        throws HibernateException {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();

        String hql = "from Complain c where c.state=:state";

        if (beginDate != null) {
            hql += " and c.complainTime >= :beginDate";
        }

        if (endDate != null) {
            hql += " and c.complainTime <= :endDate";
        }

        Query query = session.createQuery(hql);

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }

        if (endDate != null) {
            query.setDate("endDate", endDate);
        }

        query.setInteger("state", state);

        //Query query = session.createQuery("from Complain c where c.alreadyDeal is null or alreadyDeal = '?'");
        query.setMaxResults(page.getEveryPage());
        query.setFirstResult(page.getBeginIndex());
        return query.list();
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

From source file:com.dz.module.driver.complain.ComplainDaoImpl.java

@SuppressWarnings("unchecked")
@Override//  w w w.j  av a  2s. c  o  m
public List<Complain> selectByDriver(Driver driver, Page page) throws HibernateException {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("from Complain c where c.idNum = :idnum");
        query.setString("idnum", driver.getIdNum());
        query.setMaxResults(page.getEveryPage());
        query.setFirstResult(page.getBeginIndex());
        return query.list();
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

From source file:com.dz.module.driver.complain.ComplainDaoImpl.java

@SuppressWarnings("unchecked")
@Override/*w  w  w .ja va 2s. com*/
public List<Complain> selectAllByStates(Complain complain, Page page, Date beginDate, Date endDate, String dept,
        Short[] states, String order) {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();

        String hql = "select c from Complain c,Vehicle v where c.state in (:states)";

        if (beginDate != null) {
            hql += " and c.complainTime >= :beginDate";
        }

        if (endDate != null) {
            hql += " and c.complainTime <= :endDate";
        }

        if (!StringUtils.isEmpty(dept)) {
            hql += " and c.vehicleId in (select carframeNum from Vehicle where dept like :dept) ";
        }

        if (complain != null) {
            if (StringUtils.isNotEmpty(complain.getComplainClass())) {
                hql += " and c.complainClass like :complainClass";
            }

            if (StringUtils.isNotEmpty(complain.getComplainObject())) {
                hql += " and c.complainObject like :complainObject";
            }
        }

        hql += " and c.vehicleId=v.carframeNum ";

        if (StringUtils.equals(order, "complainTime")) {
            hql += " order by c.complainTime ";
        } else {
            hql += " order by v.licenseNum ";
        }

        Query query = session.createQuery(hql);

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }

        if (endDate != null) {
            query.setDate("endDate", endDate);
        }

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

        query.setParameterList("states", states);

        if (complain != null) {
            if (StringUtils.isNotEmpty(complain.getComplainClass())) {
                query.setString("complainClass", "%" + complain.getComplainClass() + "%");
            }

            if (StringUtils.isNotEmpty(complain.getComplainObject())) {
                query.setString("complainObject", "%" + complain.getComplainObject() + "%");
            }
        }
        //Query query = session.createQuery("from Complain c where c.alreadyDeal is null or alreadyDeal = '?'");
        query.setMaxResults(page.getEveryPage());
        query.setFirstResult(page.getBeginIndex());
        return query.list();
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}