Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

In this page you can find the example usage for org.hibernate Session saveOrUpdate.

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

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

public String addInvoice() {
    Invoice i = new Invoice();
    BeanUtils.copyProperties(vehicle, i, new String[] { "state" });
    i.setState(0);/*from  w  ww.j  a va  2  s  .  c  om*/
    Session s = null;
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        s.saveOrUpdate(i);
        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 revokeInvoice() {
    Session s = null;
    Transaction tx = null;/*from w w w.j  a  va  2  s.c o m*/
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Invoice v = (Invoice) s.get(Invoice.class, vehicle.getCarframeNum());
        if (v == null || v.getState() != 1) {
            request.setAttribute("msgStr", "");
            return SUCCESS;
        } else {
            v.setState(0);
            s.saveOrUpdate(v);
        }

        Vehicle vh = (Vehicle) s.get(Vehicle.class, vehicle.getCarframeNum());
        Invoice ispace = new Invoice();
        BeanUtils.copyProperties(ispace, vh, new String[] { "state", "carframeNum" });
        s.saveOrUpdate(vh);

        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 addTax() {
    Tax i = new Tax();
    BeanUtils.copyProperties(vehicle, i, new String[] { "state" });
    i.setState(0);/*ww  w.j  av a 2  s.co  m*/
    Session s = null;
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        s.saveOrUpdate(i);
        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 revokeTax() {
    Session s = null;
    Transaction tx = null;/*from  ww w  .  jav a2 s  .c  o m*/
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        Tax v = (Tax) s.get(Tax.class, vehicle.getCarframeNum());
        if (v == null || v.getState() != 1) {
            request.setAttribute("msgStr", "");
            return SUCCESS;
        } else {
            v.setState(0);
            s.saveOrUpdate(v);
        }

        Vehicle vh = (Vehicle) s.get(Vehicle.class, vehicle.getCarframeNum());
        Tax ispace = new Tax();
        BeanUtils.copyProperties(ispace, vh, new String[] { "state", "carframeNum" });
        s.saveOrUpdate(vh);

        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 addLicence() {
    Vehicle v = vehicleService.selectById(vehicle);

    if (v == null) {
        request.setAttribute("msgStr", "???");
        return SUCCESS;
    }/*from ww  w . j av  a2  s .  co  m*/

    String basePath = System.getProperty("com.dz.root") + "data/vehicle/" + vehicle.getCarframeNum() + "/";
    File base = new File(basePath);
    if (!base.exists()) {
        base.mkdirs();
    }
    if (StringUtils.length(photo) == 30) {
        FileUploadUtil.store(photo, new File(base, "photo.jpg"));
    }

    License i = new License();
    BeanUtils.copyProperties(vehicle, i, new String[] { "state" });
    i.setState(0);
    Session s = null;
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        s.saveOrUpdate(i);
        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 relookLicence() {
    Session s = null;
    Transaction tx = null;//from w ww.ja v  a2s.co  m
    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;
    Transaction tx = null;/*w  w w .ja v a2s .  c  om*/
    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;
}

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

/**
 * ?//w  w  w .  j a  v  a 2  s .  c o  m
 */
public String addService() {
    Vehicle v = vehicleService.selectById(vehicle);
    if (v == null) {
        request.setAttribute("msgStr", "???");
        return SUCCESS;
    }

    ServiceInfo i = new ServiceInfo();
    BeanUtils.copyProperties(vehicle, i, new String[] { "state" });
    i.setState(0);
    Session s = null;
    Transaction tx = null;
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        s.saveOrUpdate(i);
        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 relookService() {
    Session s = null;
    Transaction tx = null;/*from w  w  w.j  a  v  a  2 s  . co  m*/
    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 revokeService() {
    Session s = null;
    Transaction tx = null;/*ww  w.j  a  v  a  2 s.  c om*/
    try {
        s = HibernateSessionFactory.getSession();
        tx = s.beginTransaction();
        ServiceInfo i = (ServiceInfo) s.get(ServiceInfo.class, vehicle.getCarframeNum());

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

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

        ServiceInfo ispace = new ServiceInfo();
        BeanUtils.copyProperties(ispace, v, new String[] { "state", "carframeNum" });

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