Example usage for org.hibernate Query setString

List of usage examples for org.hibernate Query setString

Introduction

In this page you can find the example usage for org.hibernate Query setString.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setString(String name, String val) 

Source Link

Document

Bind a named String-valued parameter.

Usage

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

/**
 * /  ???,/*from  w  w  w . j  ava 2s.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

@Override
public long contractSearchAllAvaliableCount(Date time, String dept) {
    List<Contract> l = new ArrayList<Contract>();
    Session session = null;/* ww  w.  j ava  2s.  co  m*/
    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);
        }
        l = query.list();
        Iterator<Contract> iterators = l.iterator();
        filterContractThatStateEq1Charge(time, iterators);
        tx.commit();
        return l.size();
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        return 0;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

@Override
public Contract selectByCarId(String id) {
    Contract c = null;//from   ww w  .j  a  va  2  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;/*  ww w  .java  2s .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

@Override
public int selectAllByStatesCount(Contract contract, Vehicle vehicle, Driver driver, Date beginDate,
        Date endDate, Short[] states) {

    Session session = null;/*  w  w  w .  j a  va 2s  .  com*/
    try {
        session = HibernateSessionFactory.getSession();

        String hql = "select count(*) 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);
        return Integer.parseInt(query.uniqueResult().toString());
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

@SuppressWarnings("unchecked")
@Override//  ww w  .j  a  va 2s . co 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.contract.ContractDaoImpl.java

@Override
public int contractSearchAllAvaliableCount(Date time, String dept, String licenseNum) {
    Session session = null;//from  w  ww  . ja va  2 s  .com
    Transaction tx = null;
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();
        Query query = null;

        String hql = "select count(*) from Contract where state in (0,-1,1,4) and (abandonedFinalTime is null or (YEAR(abandonedFinalTime)*12+MONTH(abandonedFinalTime)+(case when DAY(abandonedFinalTime)>26 then 1 else 0 end) >= (YEAR(:currentClearTime)*12+MONTH(:currentClearTime))) )";
        if (dept != null && !"".equals(dept)) {
            hql += "and branchFirm = :dept ";
        }

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

        query = session.createQuery(hql);

        if (dept != null && !"".equals(dept)) {
            query.setString("dept", dept);
        }

        if (!StringUtils.isEmpty(licenseNum)) {
            query.setString("carNum", licenseNum);
        }

        query.setDate("currentClearTime", time);

        long count = (long) query.uniqueResult();
        tx.commit();
        return (int) count;
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        return 0;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

@Override
public List<Contract> contractSearchAllAvilable(Date time, String dept, String licenseNum, Page page) {
    List<Contract> l = new ArrayList<Contract>();
    Session session = null;/*from www  .ja  va  2s. c o  m*/
    Transaction tx = null;
    try {
        session = HibernateSessionFactory.getSession();
        tx = (Transaction) session.beginTransaction();
        Query query = null;

        String hql = "from Contract where state in (0,-1,1,4)  and (abandonedFinalTime is null or (YEAR(abandonedFinalTime)*12+MONTH(abandonedFinalTime)+(case when DAY(abandonedFinalTime)>26 then 1 else 0 end) >= (YEAR(:currentClearTime)*12+MONTH(:currentClearTime)) ))";
        if (dept != null && !"".equals(dept)) {
            hql += "and branchFirm = :dept ";
        }

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

        query = session.createQuery(hql);

        if (dept != null && !"".equals(dept)) {
            query.setString("dept", dept);
        }

        if (!StringUtils.isEmpty(licenseNum)) {
            query.setString("carNum", licenseNum);
        }

        query.setDate("currentClearTime", time);

        l = query.list();

        tx.commit();
        return l;
    } catch (HibernateException e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        return l;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

@SuppressWarnings("unchecked")
@Override/*from  ww  w  . ja v 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

@Override
public int selectByDriverCount(Driver driver) throws HibernateException {
    Session session = null;/*from  w  ww . ja  v a 2  s  . c o  m*/
    try {
        session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("select count(*) from Complain c where c.idNum = :idnum");
        query.setString("idnum", driver.getIdNum());
        return Integer.parseInt(query.uniqueResult().toString());
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}