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.driver.DriverDaoImpl.java

@Override
public int driverSearchConditionTotal(Date beginDate, Date endDate, Driver driver) throws HibernateException {
    Session session = null;/*from ww w.ja v a  2  s .co m*/
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "select count(*) from Driver where 1=1 ";

        if (!StringUtils.isEmpty(driver.getIdNum())) {
            sql += "and idNum like :idNum ";
        }

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

        if (!StringUtils.isEmpty(driver.getTeam())) {
            sql += "and team like :team ";
        }

        if (beginDate != null) {
            sql += "and applyTime>:beginDate ";
        }
        if (endDate != null) {
            sql += "and applyTime<:endDate ";
        }

        if (driver.getIsInCar() != null)
            sql += "and isInCar=:isInCar";

        Query query = session.createQuery(sql);

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

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

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

        if (!StringUtils.isEmpty(driver.getDept())) {
            query.setString("dept", "%" + driver.getDept() + "%");
            sql += "and dept like :dept ";
        }

        if (!StringUtils.isEmpty(driver.getTeam())) {
            query.setString("team", "%" + driver.getTeam() + "%");
            sql += "and team like :team ";
        }

        if (driver.getIsInCar() != null)
            query.setBoolean("isInCar", driver.getIsInCar());

        return Integer.parseInt(query.uniqueResult().toString());
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

From source file:com.dz.module.driver.DriverDaoImpl.java

@Override
public int selectDriverLeaveByConditionCount(Date beginDate, Date endDate, Vehicle vehicle, Driver driver,
        Boolean finished, String operation) {
    Session session = null;//from w  w w  .j a  va 2  s.c  o  m
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "select count(*) from Driverleave where finished=:finished ";

        if (beginDate != null) {
            sql += "and opeTime>:beginDate ";
        }
        if (endDate != null) {
            sql += "and opeTime<:endDate ";
        }

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

        if (!StringUtils.isEmpty(driver.getIdNum())) {
            sql += "and idNumber like :idNum ";
        }

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

        Query query = session.createQuery(sql);

        query.setBoolean("finished", finished);

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

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

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

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

        return Integer.parseInt(query.uniqueResult().toString());
    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

From source file:com.dz.module.driver.DriverDaoImpl.java

@SuppressWarnings("unchecked")
@Override// w  w w .j ava  2 s  .  c o m
public List<Driverleave> selectDriverLeaveByCondition(Page page, Date beginDate, Date endDate, Vehicle vehicle,
        Driver driver, Boolean finished, String operation) {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "from Driverleave where finished=:finished ";

        if (beginDate != null) {
            sql += "and opeTime>:beginDate ";
        }
        if (endDate != null) {
            sql += "and opeTime<:endDate ";
        }

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

        if (!StringUtils.isEmpty(driver.getIdNum())) {
            sql += "and idNumber like :idNum ";
        }

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

        Query query = session.createQuery(sql);

        query.setBoolean("finished", finished);

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

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

        if (beginDate != null) {
            query.setDate("beginDate", beginDate);
        }
        if (endDate != null) {
            query.setDate("endDate", endDate);
        }
        if (!StringUtils.isEmpty(operation)) {
            query.setString("operation", "%" + operation + "%");
        }
        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.vehicle.electric.ElectricAnaylseService.java

public int createAnaylse(Date beginDate, Date endDate) {
    Session session = HibernateSessionFactory.getSession();
    Transaction tx = null;//ww w  . j a va  2s . c o  m
    int ea_id = 0;
    try {
        tx = session.beginTransaction();

        ElectricAnaylse ea = new ElectricAnaylse();

        Query query = session.createQuery("select count(*),sum(cast(money as integer)) "
                + "from ElectricHistory " + "where STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')>=:beginDate "
                + "and STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')<=:endDate");

        Query query_dept = session.createQuery("select count(*),sum(cast(money as integer)) "
                + "from ElectricHistory " + "where STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')>=:beginDate "
                + "and STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')<=:endDate "
                + "and carframeNum in (select carframeNum from Vehicle where dept=:dept)");

        query.setDate("beginDate", beginDate);
        query.setDate("endDate", endDate);

        query_dept.setDate("beginDate", beginDate);
        query_dept.setDate("endDate", endDate);

        Object[] oarr = (Object[]) query.uniqueResult();

        ea.setBeginDate(beginDate);
        ea.setEndDate(endDate);
        ea.setAllTimes((int) (long) oarr[0]);
        ea.setAllMoney((long) oarr[1]);

        query_dept.setString("dept", "");
        oarr = (Object[]) query_dept.uniqueResult();
        ea.setTime1((int) (long) oarr[0]);
        ea.setMoney1((long) oarr[1]);

        query_dept.setString("dept", "");
        oarr = (Object[]) query_dept.uniqueResult();
        ea.setTime2((int) (long) oarr[0]);
        ea.setMoney2((long) oarr[1]);

        query_dept.setString("dept", "");
        oarr = (Object[]) query_dept.uniqueResult();
        ea.setTime3((int) (long) oarr[0]);
        ea.setMoney3((long) oarr[1]);
        session.save(ea);

        //         System.out.println("ElectricAnaylseService.createAnaylse(),line 76,ElectricAnaylse:\t"+ea);
        ea_id = ea.getId();

        Query query_vehicle = session.createQuery(
                "insert into ElectricAnaylseVehicle(anaylseId,carframeNum,licenseNum,times,moneys) " + "select "
                        + ea.getId()
                        + ",carframeNum,licenseNum,cast(count(*) as integer),sum(cast(money as integer)) from ElectricHistory "
                        + "where STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')>=:beginDate "
                        + "and STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')<=:endDate " + "group by carframeNum");

        query_vehicle.setDate("beginDate", beginDate);
        query_vehicle.setDate("endDate", endDate);

        query_vehicle.executeUpdate();

        Query query_act = session.createQuery(
                "insert into ElectricAnaylseAct(anaylseId,act,times,moneys) " + "select " + ea.getId()
                        + ",act,cast(count(*) as integer),sum(cast(money as integer)) from ElectricHistory "
                        + "where STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')>=:beginDate "
                        + "and STR_TO_DATE(date,'%Y-%m-%d %H:%i:%s')<=:endDate " + "group by act");

        query_act.setDate("beginDate", beginDate);
        query_act.setDate("endDate", endDate);

        query_act.executeUpdate();

        Query query_act_area = session
                .createQuery("insert into ElectricAnaylseActArea(anaylseActId,area,times) "
                        + "select eaa.id,eh.area,cast(count(*) as integer) "
                        + "from ElectricHistory eh,ElectricAnaylseAct eaa " + "where eaa.anaylseId=:aid "
                        + "and eaa.act=eh.act " + "and STR_TO_DATE(eh.date,'%Y-%m-%d %H:%i:%s')>=:beginDate "
                        + "and STR_TO_DATE(eh.date,'%Y-%m-%d %H:%i:%s')<=:endDate "
                        + "group by eh.act,eh.area");

        query_act_area.setInteger("aid", ea.getId());
        query_act_area.setDate("beginDate", beginDate);
        query_act_area.setDate("endDate", endDate);

        query_act_area.executeUpdate();

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

    return ea_id;
}

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

@SuppressWarnings("unchecked")
@Override/*from   w  w w .  j av a  2 s .  c  o m*/
public List<Insurance> selectByVehicle(Vehicle vehicle) {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("from Insurance where state>0 and carframeNum=:carframeNum");
        query.setString("carframeNum", vehicle.getCarframeNum());
        return query.list();

    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

@SuppressWarnings("unchecked")
@Override// w w  w.  j a va 2 s .co  m
public List<Insurance> selectByDriver(Driver driver) {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("from Insurance where state>0 and driverId=:driverId");
        query.setString("driverId", driver.getIdNum());
        return query.list();

    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

@Override
public int selectByConditionCount(Insurance insurance, Vehicle vehicle) {
    Session session = null;/*  w  ww. j av a  2  s  . co m*/
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "select count(*) from Insurance where state>0 ";

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

        if (!StringUtils.isEmpty(insurance.getInsuranceNum())) {
            sql += "and insuranceNum like :insuranceNum ";
        }

        if (!StringUtils.isEmpty(insurance.getInsuranceClass())) {
            sql += "and insuranceClass like :insuranceClass ";
        }

        if (vehicle != null) {
            if (!StringUtils.isEmpty(vehicle.getLicenseNum())) {
                sql += "and carframeNum in (select carframeNum from Vehicle where licenseNum  like :licenseNum ) ";
            }
        }

        Query query = session.createQuery(sql);

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

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

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

        if (vehicle != null) {
            if (!StringUtils.isEmpty(vehicle.getLicenseNum())) {
                query.setString("licenseNum", "%" + vehicle.getLicenseNum() + "%");
            }
        }
        return Integer.parseInt(query.uniqueResult().toString());

    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

@SuppressWarnings("unchecked")
@Override//  www.  j  a  va 2s  . co m
public List<Insurance> selectByCondition(Page page, Insurance insurance, Vehicle vehicle) {
    Session session = null;
    try {
        session = HibernateSessionFactory.getSession();
        String sql = "from Insurance where state>0 ";

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

        if (!StringUtils.isEmpty(insurance.getInsuranceNum())) {
            sql += "and insuranceNum like :insuranceNum ";
        }

        if (!StringUtils.isEmpty(insurance.getInsuranceClass())) {
            sql += "and insuranceClass like :insuranceClass ";
        }

        if (vehicle != null) {
            if (!StringUtils.isEmpty(vehicle.getLicenseNum())) {
                sql += "and carframeNum in (select carframeNum from Vehicle where licenseNum  like :licenseNum ) ";
            }
        }

        Query query = session.createQuery(sql);

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

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

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

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

        query.setFirstResult(page.getBeginIndex());
        query.setMaxResults(page.getEveryPage());
        return query.list();

    } catch (HibernateException e) {
        throw e;
    } finally {
        HibernateSessionFactory.closeSession();
    }
}

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

public String relookLicence() {
    Session s = null;//w ww  .j  av  a 2  s  . c  o  m
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Query query = s.createQuery("from License where state=0");
        List<License> is = query.list();
        for (License 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) {
                c.setCarNum(v.getLicenseNum());

                s.saveOrUpdate(c);

                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.setLicenseRegisterDate(v.getLicenseNumRegDate());
                approval.setOperateApplyDate(new Date());
                s.saveOrUpdate(approval);
            }

            Message msg = new Message();

            User u = (User) s.get(User.class, v.getLicenseRegister());
            msg.setFromUser(v.getLicenseRegister());
            msg.setTime(new Date());

            msg.setCarframeNum(v.getCarframeNum());
            msg.setType("");
            msg.setMsg(String.format(
                    "%tF %s?\n" + "%s(%s) ??",
                    msg.getTime(), u.getUname(), v.getLicenseNum(), v.getCarframeNum()));

            s.saveOrUpdate(msg);

            Query q_us = s.createQuery(
                    "from RelationUr where rid in (select rid from Role where rname = '')");
            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.VehicleAction.java

public String revokeLicence() {
    Session s = null;//  www  .  j av a2s .  c  o m
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        License i = (License) s.get(License.class, vehicle.getCarframeNum());

        if (i == null || i.getState() != 1) {
            request.setAttribute("msgStr", "");
            return SUCCESS;
        }

        Vehicle v = (Vehicle) s.get(Vehicle.class, i.getCarframeNum());

        Query q_c = s.createQuery(
                "select c from Contract c where c.state=0 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) {
            request.setAttribute("msgStr", "????");
            return SUCCESS;
        }

        License ispace = new License();
        ispace.setCarframeNum(v.getCarframeNum());
        BeanUtils.copyProperties(ispace, v, new String[] { "state" });

        i.setState(0);
        s.saveOrUpdate(i);
        s.saveOrUpdate(v);

        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;
}