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.model.manegers.ExchangeWS_maneger.java

public static void updateOrder(Orders pro) {
    Transaction trns = null;//from   w  ww. j  ava2 s.  co  m
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        session.saveOrUpdate(pro);
        session.getTransaction().commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
}

From source file:com.model.manegers.Product_maneger.java

public static void updateProduct(Products pro) {
    Transaction trns = null;/*from  w w  w  .j  av a 2s.c om*/
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        //
        Products lolo = new Products();
        lolo.setId(pro.getId());
        lolo.setName(pro.getName());
        lolo.setAvailableAmount(pro.getAvailableAmount());
        lolo.setUnit(pro.getUnit());
        lolo.setDiscription(pro.getDiscription());
        //
        session.saveOrUpdate(lolo);

        session.getTransaction().commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
}

From source file:com.model.manegers.User_maneger.java

public static void updateUser(Users pro) {
    Transaction trns = null;//from  w w w  .  j a  va  2  s. c o m
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        session.saveOrUpdate(pro);
        session.getTransaction().commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
}

From source file:com.msi.tough.engine.aws.rds.DBSnapshot.java

@Override
public CFType create0(final CallStruct call) throws Exception {
    final AccountType at = call.getAc();
    final String dbInstId = (String) call.getProperty(Constants.DBINSTANCEIDENTIFIER);
    final String snapshotId = (String) call.getProperty(Constants.DBSNAPSHOTIDENTIFIER);
    final RdsDbinstance inst = HibernateUtil.withNewSession(new Operation<RdsDbinstance>() {

        @Override/*w w  w  .ja v  a  2s .c o  m*/
        public RdsDbinstance ex(final Session s, final Object... args) throws Exception {
            return InstanceEntity.selectDBInstance(s, dbInstId, at.getId());
        }
    });

    final RdsSnapshot snapshot = new RdsSnapshot();
    snapshot.setAllocatedStorage(inst.getAllocatedStorage());
    snapshot.setAvailabilityZone(inst.getAvailabilityZone());
    snapshot.setDbinstanceClass(inst.getDbinstanceClass());
    snapshot.setDbinstanceId(inst.getDbinstanceId());
    snapshot.setDbparameterGroup(inst.getDbParameterGroup());
    snapshot.setDbsnapshotId(snapshotId);
    snapshot.setEngine(inst.getEngine());
    snapshot.setEngineVersion(inst.getEngineVersion());
    snapshot.setInstanceCreatedTime(inst.getInstanceCreateTime());
    snapshot.setLicenseModel(inst.getLicenseModel());
    snapshot.setMasterPasswd(inst.getMasterUserPassword());
    snapshot.setMasterUsername(inst.getMasterUsername());
    snapshot.setPort(inst.getPort());
    snapshot.setSnapshotCreateTime(new Date());
    snapshot.setSnapshotType("manual");
    snapshot.setStatus("creating");
    snapshot.setUserId(at.getId());

    HibernateUtil.withNewSession(new Operation<Object>() {
        @Override
        public Object ex(final Session s, final Object... args) throws Exception {
            s.saveOrUpdate(snapshot);
            return null;
        }
    });

    createDBSnapshotHelper(at, dbInstId, snapshot, inst);

    final CFType result = new CFType();
    result.setAcId(at.getId());
    result.setName(snapshotId);
    result.setCreatedTime(new Date());
    result.setUpdatedTime(new Date());
    return result;
}

From source file:com.msi.tough.engine.aws.rds.DBSnapshot.java

private void createDBSnapshotHelper(final AccountType at, final String dbIdentifier, final RdsSnapshot snapshot,
        final RdsDbinstance dbInstance) throws Exception {

    // create a new volume
    logger.debug("Creating a new volume to store the snapshot...");
    final String stackId = "rds." + at.getId() + "." + dbIdentifier + ".snapshot";
    final VolumeType volType = VolumeUtil.createVolume(at, dbIdentifier + "_snapshot",
            new TemplateContext(null), null, stackId, dbInstance.getAvailabilityZone(),
            dbInstance.getAllocatedStorage());
    logger.debug("Volume Type info: " + volType.toCFString());

    HibernateUtil.withNewSession(new Operation<Object>() {
        @Override/*w  ww. j a  va 2s .  c o m*/
        public Object ex(final Session s, final Object... args) throws Exception {
            snapshot.setVolumeId(volType.getVolumeId());
            s.saveOrUpdate(snapshot);
            return null;
        }
    });

    // attach the volume
    /*logger.debug("Attaching the volume to the DBInstance...");
    final CallStruct attachCall = new CallStruct();
    attachCall.setAc(at);
    final HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put("AvailabilityZone", dbInstance.getAvailabilityZone());
    properties.put("InstanceId", dbInstance.getInstanceId());
    properties.put("VolumeId", volType.getVolumeId());
    final String device = (String) ConfigurationUtil.getConfiguration(Arrays
    .asList(new String[] { "VirtualDisk",
          dbInstance.getAvailabilityZone() }));
    if(device == null){
       throw RDSQueryFaults.InternalFailure();
    }
    properties.put("Device", device);
    attachCall.setProperties(properties);
    new Volume().attach(attachCall);
    logger.debug("Finished attaching the volume to the DBInstance...");*/

    final String s1 = "{\"DBSnapshotIdentifier\":\"" + snapshot.getDbsnapshotId() + "\"}";
    ChefUtil.createDatabagItem("rds-" + at.getId() + "-" + dbIdentifier, "DBSnapshot", s1);

}

From source file:com.mtech.springsecurity.dao.AccountingPeriodDaoImpl.java

public boolean saveAccountingPeriods(List<AccountingPeriod> aps) {
    Session iSess = getSession();
    for (AccountingPeriod ap : aps) {
        iSess.saveOrUpdate(ap);
    }//from w ww.  ja va 2s .  c o  m
    iSess.flush();
    return true;

}

From source file:com.mtech.springsecurity.dao.BankDaoImpl.java

public BankBranch saveBankBranch(BankBranch bankBranch) {
    Session bankBranchSession = getSession();
    if (bankBranch.getId() == null) {
        bankBranchSession.saveOrUpdate(bankBranch);
    } else {/* w w  w  .jav  a 2s  .  c o  m*/
        bankBranchSession.merge(bankBranch);
    }
    bankBranchSession.flush();
    return bankBranch;
}

From source file:com.muslim.family.dao.impl.AnswerDAOImpl.java

public void saveOrUpdateAudioDao(MultipartFile audioFile, Answer_tbl answer) {

    try {/*from w w  w. j  av  a 2 s .  c om*/

        Session session = sessionFactory.getCurrentSession();
        Blob audioMessage = Hibernate.getLobCreator(session).createBlob(audioFile.getBytes());
        answer.setAudioMessage(audioMessage);
        session.saveOrUpdate(answer);

    } catch (IOException ex) {
        Logger.getLogger(AnswerDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.muslim.family.dao.impl.AnswerDAOImpl.java

public void saveOrUpdateShaikhTextDao(Answer_tbl answer) {

    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(answer);

}

From source file:com.mycompany.controller.account.LoginController.java

License:Apache License

@RequestMapping(value = "/login/resetPassword", method = RequestMethod.POST)
public String processResetPassword(@ModelAttribute("resetPasswordForm") ResetPasswordForm resetPasswordForm,
        HttpServletRequest request, HttpServletResponse response, Model model, BindingResult errors)
        throws ServiceException {
    GenericResponse errorResponse = new GenericResponse();
    resetPasswordValidator.validate(resetPasswordForm.getUsername(), resetPasswordForm.getPassword(),
            resetPasswordForm.getPasswordConfirm(), errors);
    if (errorResponse.getHasErrors()) {
        return getResetPasswordView();
    }/* www.ja  v  a 2s .c o  m*/

    errorResponse = new GenericResponse();
    Customer customer = null;
    if (resetPasswordForm.getUsername() != null) {
        customer = customerDao.readCustomerByUsername(resetPasswordForm.getUsername());
    }
    checkCustomer(customer, errorResponse);
    checkPassword(resetPasswordForm.getPassword(), resetPasswordForm.getPasswordConfirm(), errorResponse);
    CustomerForgotPasswordSecurityToken fpst = checkPasswordResetToken(resetPasswordForm.getToken(),
            errorResponse);

    if (!errorResponse.getHasErrors()) {
        if (!customer.getId().equals(fpst.getCustomerId())) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Password reset attempt tried with mismatched customer and token " + customer.getId()
                        + ", " + resetPasswordForm.getToken());
            }
            errorResponse.addErrorCode("invalidToken");
        }
    }

    if (!errorResponse.getHasErrors()) {
        customer.setUnencodedPassword(resetPasswordForm.getPassword());
        saveCustomer(customer, true);
        fpst.setTokenUsedFlag(true);
        //            customerForgotPasswordSecurityTokenDao.saveToken(fpst);
        Session hSession = em.unwrap(Session.class);
        hSession.saveOrUpdate(fpst);
        hSession.flush();
    }

    if (errorResponse.getHasErrors()) {
        String errorCode = errorResponse.getErrorCodesList().get(0);
        request.setAttribute("errorCode", errorCode);
        return getResetPasswordView();
    } else {
        // The reset password was successful, so log this customer in.          
        Authentication auth = loginService.loginCustomer(resetPasswordForm.getUsername(),
                resetPasswordForm.getPassword());
        mergeCartProcessor.execute(request, response, auth);

        return getResetPasswordSuccessView();
    }

    //        return super.processResetPassword(resetPasswordForm, request, response, model, errors);
}