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:com.persistencia.AlmacenDAO.java

public void guardaAlmacen(Almacen almacen) throws Exception {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    try {//from www. j a  v  a2s.  com
        session.beginTransaction();
        if (almacen.getIdAlmacen() == 0) {
            session.save(almacen);
        } else {
            Almacen clienteTmp = (Almacen) session.merge(almacen);
            session.update(clienteTmp);
        }
        session.getTransaction().commit();
    } catch (Exception ex) {
        session.getTransaction().rollback();
        Logger.getLogger(AlmacenDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        session.close();
    }
}

From source file:com.persistencia.AlmacenDAO.java

public void eliminar(Almacen almacen) throws Exception {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();

    try {//from   w  ww  .ja va2s.  c om

        session.beginTransaction();
        Almacen almacentmp = (Almacen) session.merge(almacen);
        session.delete(almacentmp);

        session.getTransaction().commit();
    } catch (Exception ex) {
        session.getTransaction().rollback();
        Logger.getLogger(AlmacenDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        session.close();
    }

}

From source file:com.persistencia.ProductoDAO.java

public void guardar(Producto prod) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    try {/*ww w  .j a  va 2  s.com*/
        session.beginTransaction();
        if (prod.getIdProducto() == 0) {
            session.save(prod);
        } else {
            Producto prodTmp = (Producto) session.merge(prod);
            session.update(prodTmp);
        }
        session.getTransaction().commit();
    } catch (Exception ex) {
        session.getTransaction().rollback();
        Logger.getLogger(ProductoDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        session.close();
    }

}

From source file:com.persistencia.ProductoDAO.java

public void eliminar(Producto prod) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();

    try {//from  ww w . j  a v a  2  s. c  o m

        session.beginTransaction();
        Producto prodTmp = (Producto) session.merge(prod);
        session.delete(prodTmp);

        session.getTransaction().commit();
    } catch (Exception ex) {
        session.getTransaction().rollback();
        Logger.getLogger(ProductoDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        session.close();
    }

}

From source file:com.persistencia.ProveedorDAO.java

public void guardaProveedor(Proveedor prov) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    try {/*w ww  . j a v  a2 s . co m*/
        session.beginTransaction();
        if (prov.getIdProveedor() == 0) {
            session.save(prov);
        } else {
            Proveedor provTmp = (Proveedor) session.merge(prov);
            session.update(provTmp);
        }
        session.getTransaction().commit();
    } catch (Exception ex) {
        session.getTransaction().rollback();
        Logger.getLogger(ProveedorDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        session.close();
    }

}

From source file:com.persistencia.ProveedorDAO.java

public void eliminarProveedor(Proveedor prov) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();

    try {/*  w ww  .j a  v a 2  s .c o  m*/

        session.beginTransaction();
        Proveedor provTmp = (Proveedor) session.merge(prov);
        session.delete(provTmp);

        session.getTransaction().commit();
    } catch (Exception ex) {
        session.getTransaction().rollback();
        Logger.getLogger(ProveedorDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        session.close();
    }

}

From source file:com.project.dao.Dao.java

@Override
public boolean merge(T entity) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {// w  w  w. j ava 2 s.c o  m

        session.getTransaction().begin();
        session.merge(entity);
        session.getTransaction().commit();
        FacesUtil.addInfoMessage("Sincronizado com sucesso!!");
        return true;
    } catch (Exception e) {
        session.getTransaction().rollback();
        FacesUtil.addInfoMessage("Erro ao sincronizar!!!" + e);
        return false;
    } finally {
        if (session.isOpen()) {
            session.close();
        }
    }
}

From source file:com.projetIF4.controller.ProjetProposeControleur.java

/**
 *
 *///from w w w.  j  ava 2  s  . c o m
public void update() {
    final Session session = HibernateUtil.currentSession();

    try {
        final Transaction transaction = session.beginTransaction();

        try {
            for (Projetpropose listProjetpropose : projets) {
                session.merge(listProjetpropose);
            }

            transaction.commit();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Succs", "Mise a jour des projets avec succes !"));
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Erreur", "Mise  jour des projets a chou !" + ex));
            transaction.rollback();

            throw ex;
        }
    } finally {
        HibernateUtil.closeSession();
    }
}

From source file:com.qa.onlinebanking.dao.ObjectDao.java

public void updateObject(Object object, long id, Class<T> ClassName)
        throws IllegalAccessException, InvocationTargetException {
    Session session = factory.openSession();
    Transaction tx = null;//from   ww w .  j  av a2s .c  om
    try {
        session.evict(object);
        tx = session.beginTransaction();
        // Update Object

        this.t = (T) session.get(ClassName, id);
        BeanUtils.copyProperties(t, object);
        session.merge(t);
        //session.update(object);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
}

From source file:com.ravipatel.statusserver.models.LicenseUserCompanyDao.java

License:Open Source License

public LicenseUserCompany merge(LicenseUserCompany detachedInstance) {
    log.debug("merging LicenseUserCompany instance");
    try {//  w w  w  .j a  v  a2 s  .c om
        Session session = sessionFactory.getCurrentSession();
        LicenseUserCompany result = (LicenseUserCompany) session.merge(detachedInstance);
        log.debug("merge successful");
        return result;
    } catch (RuntimeException re) {
        log.error("merge failed", re);
        throw re;
    }
}