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.mycompany.controller.account.LoginController.java

License:Apache License

public GenericResponse sendForgotPasswordNotification(String username, String resetPasswordUrl) {
    GenericResponse response = new GenericResponse();
    Customer customer = null;/*from  w ww . j a  va  2 s.com*/

    if (username != null) {
        customer = customerService.readCustomerByUsername(username);
    }

    checkCustomer(customer, response);

    if (!response.getHasErrors()) {
        String token = PasswordUtils.generateTemporaryPassword(20);
        token = token.toLowerCase();

        CustomerForgotPasswordSecurityToken fpst = new CustomerForgotPasswordSecurityTokenImpl();
        fpst.setCustomerId(customer.getId());

        UserDetails principal = userDetailsService.loadUserByUsername(username);
        salt = new SaltSource() {
            @Override
            public Object getSalt(UserDetails user) {
                return "mir!";
            }
        };

        fpst.setToken(passwordEncoder.encodePassword(token, salt.getSalt(principal)));
        fpst.setCreateDate(SystemTime.asDate());

        //GK: custom database insert/update
        //GK: custom query
        Session hSession = em.unwrap(Session.class);
        hSession.saveOrUpdate(fpst);
        hSession.flush();

        //            fpst = customerForgotPasswordSecurityTokenDao.saveToken(fpst);

        HashMap<String, Object> vars = new HashMap<String, Object>();
        vars.put("token", token);
        if (!StringUtils.isEmpty(resetPasswordUrl)) {
            if (resetPasswordUrl.contains("?")) {
                resetPasswordUrl = resetPasswordUrl + "&token=" + fpst.getToken();
            } else {
                resetPasswordUrl = resetPasswordUrl + "?token=" + fpst.getToken();
            }
        }
        vars.put("resetPasswordUrl", resetPasswordUrl);

        try {
            registrationEmailService.sendPasswordEmail(customer.getEmailAddress(), vars);
        } catch (Exception e) {
            //Problem with sending email
            e.printStackTrace();
        }
    }
    return response;
}

From source file:com.mycompany.desktopinlamninguppgift2.repository.DeveloperRepository.java

public Developer addDeveloper(Developer developer) {

    Session session = NewHibernateUtil.getSession();
    session.beginTransaction();//from w ww.  j  a v  a  2 s  .  co  m
    Developer myDev = new Developer();
    myDev.setDeveloperName(developer.getDeveloperName());
    session.save(myDev);
    List<Game> games = (List<Game>) developer.getGames();
    myDev.setGames(games);
    session.saveOrUpdate(myDev);
    session.getTransaction().commit();
    session.close();

    return myDev;
}

From source file:com.mycompany.restful.dao.FeatureDao.java

public boolean saveFeature(Feature feature) {
    Session session = null;
    boolean hasErrors = false;

    try {//w  ww  . j a va 2 s  . c  o  m
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.saveOrUpdate(feature);
        session.getTransaction().commit();
    } catch (Exception ex) {
        if (session != null) {
            session.getTransaction().rollback();
            hasErrors = true;
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return hasErrors;
}

From source file:com.mycompany.testws.dao.PersonDao.java

public boolean savePerson(Person person) {
    Session session = null;
    boolean hasErrors = false;

    try {//from w  w  w  .jav a 2s  .c  om
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.saveOrUpdate(person);
        session.getTransaction().commit();

    } catch (Exception ex) {
        if (session != null) {
            session.getTransaction().rollback();
            Log.write("   savePerson ");
        }
        hasErrors = true;

    } finally {
        if (session != null) {
            session.close();
        }
    }
    return hasErrors;
}

From source file:com.myproject.onlinecounter.dao.impl.CategoryDaoImpl.java

@Override
public void updateCategory(Category category) {
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(category);
}

From source file:com.myproject.onlinecounter.dao.impl.OrderDaoImpl.java

@Override
public void updateOrder(Orders order) {
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(order);
}

From source file:com.myproject.onlinecounter.dao.impl.ProductDaoImpl.java

@Override
public void updateProduct(Product product) {
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(product);
}

From source file:com.naval.persistencia.dao.impl.GenericDAOImplHibernate.java

@Override
public void saveOrUpdate(T entity) throws BussinessException {
    Session session = sessionFactory.getCurrentSession();
    try {//from  w  w  w .ja v  a2s.c o  m
        session.beginTransaction();
        session.saveOrUpdate(entity);
        session.getTransaction().commit();
    } catch (javax.validation.ConstraintViolationException cve) {
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Exception exc) {
            LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc);
        }
        throw new BussinessException(cve);
    } catch (org.hibernate.exception.ConstraintViolationException cve) {
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Exception exc) {
            LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc);
        }
        throw new BussinessException(cve);
    } catch (RuntimeException ex) {
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Exception exc) {
            LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc);
        }
        throw ex;
    } catch (Exception ex) {
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Exception exc) {
            LOGGER.log(Level.WARNING, "Fall al hacer un rollback", exc);
        }
        throw new RuntimeException(ex);
    }
}

From source file:com.nbr.nfo.db.model.repo.UserRepository.java

@Transactional(readOnly = false)
public void addUser(User user) {
    Session session = sessionFactory.openSession();
    //        session.getTransaction().begin();
    session.saveOrUpdate(user);
    //        session.getTransaction().commit();
    session.close();/*from  ww  w.  j  a  va 2s.  c  o m*/
}

From source file:com.nec.crud.CrudRepositoryImpl.java

License:Open Source License

/** {@inheritDoc} */
@Override/*  w w w.  jav a2  s.  c o m*/
public void saveOrUpdate(final Session session, T entity) {
    session.saveOrUpdate(entity);
}