Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

From source file:br.sp.unifae.cris.comp7.model.dao.DAOProduto.java

public void alterar(Produto produto) {

    Session session = DAOHibernateUtil.getSession();

    try {// w  ww . j a  va2s  .  co  m

        session.beginTransaction(); // Abre-se uma transao
        session.merge(produto); // Acumula a operao de alterao do objeto produto no BD, na transao
        session.getTransaction().commit(); // Realiza definitivamente todas as operaes pendentes na transao
        JOptionPane.showMessageDialog(null, "Produto alterado com sucesso");

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);

    }
}

From source file:br.ufg.inf.es.fs.contpatri.persistencia.dao.GenericDAOImpl.java

License:GNU General Public License

@Override
public void merge(T entity) {
    try {/*from   www . j a v a 2s. c  om*/
        HibernateUtil.beginTransaction();
        Session hibernateSession = this.getSession();
        hibernateSession.merge(entity);
        HibernateUtil.commitTransaction();
    } catch (HibernateException e) {
        HibernateUtil.rollbackTransaction();
        throw e;
    }
}

From source file:br.ufg.inf.es.fs.contpatri.web.persistencia.dao.GenericDAOImpl.java

License:GNU General Public License

@Override
public void merge(T entity) {
    Session hibernateSession = this.getSession();
    hibernateSession.merge(entity);
}

From source file:br.ufmt.ic.paw2.DAO.PacienteDAO.java

public void gravar(final Paciente paciente) {

    Paciente merge;/*from  w w w.  j av  a  2s. c o m*/

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();

    if ((paciente != null) && (paciente.getId() > 0)) {
        merge = (Paciente) session.merge(paciente);
        session.persist(merge);
    }

    else
        session.persist(paciente);

    t.commit();

}

From source file:br.ufmt.ic.paw2.DAO.PacienteDAO.java

public void excluir(final Paciente paciente) {

    Paciente merge;/*from   ww w  .  j av  a 2  s  .c o m*/

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();

    if ((paciente != null) && (paciente.getId() > 0)) {
        merge = (Paciente) session.merge(paciente);
        session.delete(merge);
    }

    t.commit();

}

From source file:br.unisal.dao.GenericDao.java

public <T> T update(T s) {
    Session session = HibernateUtil.getInstance().getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    try {/*w  w w  .ja v a  2s . c  o  m*/
        tx.begin();
        T save = (T) session.merge(s);
        tx.commit();
        return save;
    } catch (HibernateException e) {
        tx.rollback();
        throw new RuntimeException(e);
    } finally {
        session.close();
    }
}

From source file:business.AccountDB.java

public static String syncAccount(Account a) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {/*from   ww w.ja  va2  s . c o  m*/
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.merge(a);
        session.getTransaction().commit();
        session.flush();
        session.refresh(a);
        msg = "Account Synced!";
    } catch (HibernateException e) {
        msg = "Error Syncing Account: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        session.close();
    }

    return msg;
}

From source file:ca.qc.cegepoutaouais.tge.pige.server.UserServiceImpl.java

License:Open Source License

@Override
public void addLoans(Set<Loan> loans) throws PermissionDeniedException, FrozenAccountException {

    HttpSession httpSession = this.getThreadLocalRequest().getSession();

    logger.info("Ajout de demandes d'emprunt au compte associ au " + "thead courant... Session id: "
            + httpSession.getId());/*from   www . jav  a  2s  .  c  om*/

    Session session = null;
    Transaction tx = null;

    try {

        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        User user = (User) session.createCriteria(User.class)
                .add(Restrictions.eq(User.SID_REF, httpSession.getId())).uniqueResult();

        logger.info("Ajout des emprunts au compte suivant: " + user.asString());

        if (!user.getRole().getCanLoan() && !user.getRole().getManageLoan()) {
            logger.info("Ce compte n'a pas la permission pour effectuer cette " + "action. Compte: "
                    + user.asString());
            throw new PermissionDeniedException();
        }

        if (user.getStatus().equals(UserStatus.STATUS_FROZEN)) {
            logger.info("Ce compte ne peut pas emprunter car il est gel. " + "Compte: " + user.asString());
            throw new FrozenAccountException();
        }

        // Pour crer des emprunts pour un autre compte que le sien.
        processAlternateLoans(loans, session);

        for (Loan l : loans) {
            if (l.getId() != null) {
                session.merge(l);
            } else {
                user.addLoan(l);
            }
        }

        tx.commit();
        logger.info("Ajout russi!");
    } catch (HibernateException ex) {
        logger.error(ex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:ca.usask.gmcte.currimap.action.CharacteristicManager.java

License:Open Source License

public boolean updateCharacteristicType(String id, String name, String questionDisplay, String valueType) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from www. ja  v  a  2  s  .  com
    try {
        CharacteristicType c = (CharacteristicType) session.get(CharacteristicType.class, Integer.parseInt(id));
        c.setName(name);
        c.setQuestionDisplay(questionDisplay);
        c.setValueType(valueType);
        session.merge(c);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.CharacteristicManager.java

License:Open Source License

public boolean saveCharacteristicNameById(String newName, String id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from   w  w  w.j a va2 s. c  o  m
    try {
        Characteristic c = this.getCharacteristicById(Integer.parseInt(id), session);
        c.setName(newName);
        c.setDescription("");
        session.merge(c);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}