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.helper.GetVdtlData.java

public static void SetDonorData(List<Tbldonor> donorList) throws SQLException {
    //        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    //        String url = "jdbc:mysql://localhost:3306/templelatest?zeroDateTimeBehavior=convertToNull";
    //        Connection connection = DriverManager.getConnection(url, "root", "nitin");
    //        System.out.println("templetest connected");
    //        Statement statement = connection.createStatement();
    //        //  w  w w.j  a va2s.c  o m
    //-------
    SessionFactory newsessionFactory = NewHibernateUtil.getNewSessionFactory();
    Session session = newsessionFactory.openSession();

    Transaction newTra = session.beginTransaction();
    SessionFactory oldsessionfactory = NewHibernateUtil.getOldSessionFactory();
    Session oldSession = oldsessionfactory.openSession();
    Transaction oldtra = oldSession.beginTransaction();
    for (int i = 0; i < donorList.size(); i++) {
        System.out.println("-----------------inside for loop---->" + i);
        Tbldonor donor = donorList.get(i);
        Set<Tbldonationdetail> donationdetails = donor.getTbldonationdetails();
        Iterator<Tbldonationdetail> iterator = donationdetails.iterator();
        int totalamount = 0;
        while (iterator.hasNext()) {
            Tbldonationdetail tbldonationdetail = iterator.next();
            totalamount = totalamount + tbldonationdetail.getAmount();
            // tbldonationdetail.ge
        }
        String city = donor.getTblcity().getName();
        String area = donor.getTblarea().getName();
        String firtname = donor.getFirstname();
        String lastname = donor.getLastname();
        String address = donor.getAddress();
        String email = donor.getEmail();
        int id = donor.getId();
        String mobile = donor.getMobile();

        //saving data in othere table
        UserMaster user = new UserMaster();
        user.setFirstName(firtname);
        user.setLastName(lastname);
        user.setEmailid(email);
        user.setUsername(firtname + lastname);
        user.setPassword("111111");
        user.setMobileNo(Double.parseDouble(mobile));
        user.setTempleMasterId(1);
        user.setRoleId(0);
        user.setMakerId(0);
        Date d = new Date();
        user.setSystemDatetime(d);
        user.setDRecordEnddate(d);
        user.setDRecordStartdate(d);

        //
        System.out.println("-------saving user");
        session.saveOrUpdate(user);
        System.out.println("------------user saved");
        //address data
        UserAddressInfo userAddressInfo = new UserAddressInfo();
        userAddressInfo.setAddress(address);
        userAddressInfo.setUserMaster(user);
        userAddressInfo.setAreaId(donor.getTblarea().getId());
        userAddressInfo.setCityId(0);
        userAddressInfo.setCountryId(0);
        userAddressInfo.setDRecordEnddate(new Date());
        userAddressInfo.setDRecordStartdate(new Date());
        userAddressInfo.setPincode(0);
        userAddressInfo.setStateId(0);
        userAddressInfo.setSystemDatetime(new Date());
        System.out.println("------------saving userAddressInfo");
        session.saveOrUpdate(userAddressInfo);
        System.out.println("-------------userAddressInfo saved");

        //donationReciptmaster
        //select sum(amount),receiptno,donorid,accountid,userid,productid,qty,date from tbldonationdetail where donorid=22 group by receiptno;
        List<Object[]> datalist = oldSession.createSQLQuery(
                "select sum(amount),receiptno,donorid,accountid,userid,productid,qty,date from tbldonationdetail where donorid="
                        + donor.getId() + " group by receiptno")
                .list();
        for (int j = 0; j < datalist.size(); j++) {
            System.out.println("--------------inside inner for loop --->" + j);
            DonationReceiptInfo donationReceiptInfo = new DonationReceiptInfo();
            Object[] data = datalist.get(j);
            donationReceiptInfo.setAccountManagementMaster(
                    new AccountManagementMaster((int) data[5], "", 1, 0, (byte) 0, new Date(), ""));
            donationReceiptInfo.setAmount(totalamount);
            donationReceiptInfo.setGuestDonorName("");
            donationReceiptInfo.setPrasadId((Integer) data[5]);
            String receiptid = (String) data[1];
            if (receiptid != "") {
                donationReceiptInfo.setReceiptId(Integer.parseInt((String) data[1]));
            } else {
                donationReceiptInfo.setReceiptId(0);
            }

            donationReceiptInfo.setTempleId(1);
            System.out.println("------------saving donationReceiptInfo");
            //               session.saveOrUpdate(donationReceiptInfo);
            System.out.println("----------------------donationReceiptInfo saved");
        }

        //user info data
        UserInfo userinfo = new UserInfo();
        userinfo.setAddress(address);
        userinfo.setUserMaster(user);
        userinfo.setDob("");
        userinfo.setFacebookId("");
        userinfo.setDrivingLicenceNo("");
        userinfo.setImage("");
        userinfo.setIsDeleted(0);
        userinfo.setLandlineNo(0);
        userinfo.setPancardNo("");
        userinfo.setElectionCardNo("");
        userinfo.setAdharCardNo("");

        System.out.println("-------------------saving userinfo");
        session.saveOrUpdate(userinfo);
        System.out.println("-------------------userinfo saved");

    }
    newTra.commit();
    oldtra.commit();
}

From source file:com.helper.GetVdtlData.java

private static void setArea(List<Tblarea> areaList) {
    System.out.println("---seting area size---->" + areaList.size());
    SessionFactory newsessionFactory = NewHibernateUtil.getNewSessionFactory();
    Session session = newsessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    for (int i = 0; i < areaList.size(); i++) {
        AreaManagement areaManagement = new AreaManagement();
        Tblarea arae = areaList.get(i);/* w  w  w. ja  va 2  s. co  m*/
        System.out.println("area id----->" + arae.getId() + "----name---" + arae.getName());
        areaManagement.setAreaName(arae.getName());
        areaManagement.setDRecordEnddate(new Date());
        areaManagement.setDRecordStartdate(new Date());
        areaManagement.setMakerId(0);
        areaManagement.setSystemDatetime(new Date());
        areaManagement.setIsDeleted((byte) 0);
        session.saveOrUpdate(areaManagement);
        System.out.println("---------------------saved");

    }
    transaction.commit();
    session.close();
    newsessionFactory.close();
}

From source file:com.hihsoft.sso.business.service.impl.TsysOrgServiceImpl.java

License:Apache License

/**
 * ?TsysOrg?/*from  w  ww .j av  a  2s .c  om*/
 * 
 * @param tsysOrg
 * @throws DataAccessException
 */
public void saveOrUpdateTsysOrg(final TsysOrg tsysOrg) throws ServiceException {
    baseDAO.doInHibernate(new HibernateCallback<Void>() {
        public Void doInHibernate(Session session) throws HibernateException, SQLException {
            Transaction tx = session.beginTransaction();
            if (StringHelpers.notNull(tsysOrg.getOrgid())) {
                //??? ? ??? 
                List<TsysOrg> tsysOrgs = (List<TsysOrg>) session.createQuery(" from TsysOrg where orgno = '"
                        + tsysOrg.getOrgno() + "' and orgid != '" + tsysOrg.getOrgid() + "'").list();
                if (tsysOrgs.size() > 0) {
                    throw new ServiceException("??!");
                }
                TsysOrg old = (TsysOrg) session.get(TsysOrg.class, tsysOrg.getOrgid());
                TsysOrg parent = (TsysOrg) session.get(TsysOrg.class, tsysOrg.getParentorgid());
                BeanUtils.bean2Bean(tsysOrg, old);
                old.getTsysOrg().getTsysOrgs().remove(old);
                parent.getTsysOrgs().add(old);
                old.setTsysOrg(parent);
                session.saveOrUpdate(parent);
                session.saveOrUpdate(old);
                String regionId = null;
                if (Integer.valueOf(old.getOrgClass()) <= 2 && old.getOrgSort().equals("1")) {
                    regionId = old.getOrgid();
                } else
                    while (parent != null) {
                        if (Integer.valueOf(parent.getOrgClass()) <= 2 && parent.getOrgSort().equals("1")) {
                            regionId = parent.getOrgid();
                            break;
                        }
                        parent = parent.getTsysOrg();
                    }
                if (StringHelpers.notNull(regionId)) {
                    old.setOrgRegion(regionId);
                }
                session.saveOrUpdate(old);
            } else {
                //????
                List<TsysOrg> tsysOrgs = (List<TsysOrg>) session
                        .createQuery(" from TsysOrg where orgno = '" + tsysOrg.getOrgno() + "'").list();
                if (tsysOrgs.size() > 0) {
                    throw new ServiceException("??!");
                }
                session.saveOrUpdate(tsysOrg);
            }
            tx.commit();
            return null;
        }

    });
}

From source file:com.hihsoft.sso.business.service.impl.TsysOrgServiceImpl.java

License:Apache License

/**
 * TsysOrg?//  w  w w  . ja v a  2 s.c om
 * 
 * @param id
 * @throws DataAccessException
 * @author Xiaojf
 * @since 20110624
 */
public String deleteTsysOrg(final String id) throws ServiceException {
    return baseDAO.doInHibernate(new HibernateCallback<String>() {
        public String doInHibernate(Session session) throws HibernateException, SQLException {
            TsysOrg org = (TsysOrg) session.get(TsysOrg.class, id);
            Set<TsysOrg> subOrgs = org.getTsysOrgs();
            if (subOrgs.size() > 0) {
                for (TsysOrg o : subOrgs) {
                    //                  if ("2".equals(o.getOrgSort())) return "hasDept";
                    if ("01".equals(o.getOrgstate()))
                        return "hasOrg";
                }
            }
            int num = getDataTotalNum("from TaclUserinfo where orgid=?", id);
            if (num > 0)
                return "hasUser";
            String hql = "delete from TsysDataprivilege where orgid in (";
            StringBuffer sb = new StringBuffer();
            eachOrg(org, sb);
            hql += sb.substring(1) + ")";
            session.createQuery(hql).executeUpdate();
            org.setOrgstate(Constant.ORG_STATUS_STOPED);
            session.saveOrUpdate(org);
            hql = "delete from TsysTreeprivilege where orgid in (" + sb.substring(1) + ")";
            session.createQuery(hql).executeUpdate();

            return "success";
        }
    });
}

From source file:com.hp.dao.CustomerImageDAOImpl.java

@Override
public boolean saveOrUpdate(CustomerImage pCustomerImage) {
    Session session = getSessionFactory().openSession();
    Transaction transaction;/*  w ww.  ja v  a2s .co  m*/
    transaction = session.beginTransaction();
    try {

        session.saveOrUpdate(pCustomerImage);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        transaction.rollback();
        return false;
    } finally {
        session.close();
    }

    return true;
}

From source file:com.hp.dao.RoadManagementDAOImpl.java

@Override
public boolean saveOrUpdate(RoadManagement pRoadManagement) {
    Session session = getSessionFactory().openSession();
    Transaction transaction;// ww  w. j av  a2s.co m
    transaction = session.beginTransaction();
    try {

        session.saveOrUpdate(pRoadManagement);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        transaction.rollback();
        return false;
    } finally {
        session.close();
    }

    return true;
}

From source file:com.hp.dao.ScheduleDAOImpl.java

@Override
public boolean saveOrUpdate(Schedule pSchedule) {
    Session session = getSessionFactory().openSession();
    Transaction transaction;/*www  .  j a  v  a  2  s  . co  m*/
    transaction = session.beginTransaction();
    try {

        session.saveOrUpdate(pSchedule);
        session.getTransaction().commit();
    } catch (HibernateException e) {
        transaction.rollback();
        return false;
    } finally {
        session.close();
    }

    return true;
}

From source file:com.hp.dao.StockDAOImpl.java

public boolean saveOrUpdate(Stock pStock) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    Transaction transaction;//from   ww  w.ja  v  a 2  s .co m

    transaction = session.beginTransaction();
    try {
        //            if(getStock(pStock.getmStockID()) == null)
        //                session.save(pStock);
        //            else
        session.saveOrUpdate(pStock);
        //session.flush();
        session.getTransaction().commit();
    } catch (HibernateException e) {
        transaction.rollback();
        return false;
    } finally {
        session.close();
    }

    return true;
}

From source file:com.hyron.poscafe.dao.BarcodeDaoImpl.java

License:Open Source License

@Override
public void saveOrUpdate(Barcode entity, Session session) throws HibernateException {
    log.debug("saving or updating Barcode instance");
    try {/*from ww w .  j a va 2  s . c  o  m*/
        session.saveOrUpdate(entity);
        log.debug("save or update successful");
    } catch (RuntimeException re) {
        log.error("save or update failed", re);
        throw re;
    }
}

From source file:com.hyron.poscafe.dao.CashDrawerOptDaoImpl.java

License:Open Source License

@Override
public void saveOrUpdate(CashDrawerOpt entity, Session session) throws HibernateException {
    log.debug("saving or updating CashDrawerOpt instance");
    try {//from w  w  w.j ava2s .  c  o m
        session.saveOrUpdate(entity);
        log.debug("save or update successful");
    } catch (RuntimeException re) {
        log.error("save or update failed", re);
        throw re;
    }
}