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:daoImp.UtenteDaoImp.java

@Override
public void alterar(UteUtente utente) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {/*w w w.j a v a  2 s.  co m*/
        Transaction tx = session.beginTransaction();
        session.merge(utente);
        tx.commit();
        msg = "Utente alterada com sucesso!";
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null);
        FacesContext.getCurrentInstance().addMessage(null, message);
        RequestContext.getCurrentInstance().execute("PF('dlgEditar').hide()");

    } catch (Exception e) {
        msg = "Erro ao alterar a utente!";
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
        FacesContext.getCurrentInstance().addMessage(null, message);
        System.out.println("Erro ao Incluir o Item: " + e.getMessage());
        session.getTransaction().rollback();
        RequestContext.getCurrentInstance().execute("PF('dlgEditar').show()");

    } finally {
        try {
            session.isOpen();
            session.close();
            System.out.println("Sesso fechada");
        } catch (Exception e) {
            System.out.println("Erro: " + e.getMessage());

        }
    }
}

From source file:daoImp.UtenteDaoImp.java

@Override
public void alterarDeAddArquivo(UteUtente utente) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {/*from w w  w  . j  a va2s.c  o m*/
        Transaction tx = session.beginTransaction();
        session.merge(utente);
        tx.commit();

    } catch (Exception e) {
        System.out.println("Erro ao Incluir o Item: " + e.getMessage());
        session.getTransaction().rollback();
        RequestContext.getCurrentInstance().execute("PF('dlgEditar').show()");

    } finally {
        try {
            session.isOpen();
            session.close();
            System.out.println("Sesso fechada");
        } catch (Exception e) {
            System.out.println("Erro: " + e.getMessage());

        }
    }
}

From source file:daoImp.UtenteDaoImp.java

@Override
public void alterarActualizar(UteUtente utente) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {//from   ww w .j  a va 2 s .c om
        Transaction tx = session.beginTransaction();
        session.merge(utente);
        tx.commit();

    } catch (Exception e) {

        System.out.println("Erro ao Incluir o Item: " + e.getMessage());
        session.getTransaction().rollback();

    } finally {
        try {
            session.isOpen();
            session.close();
            System.out.println("Sesso fechada");
        } catch (Exception e) {
            System.out.println("Erro: " + e.getMessage());

        }
    }
}

From source file:DAOs.MovArticuloDAO.java

public boolean update(MovArticulo ar) {
    Transaction tr = null;//from w  ww  .  jav  a  2  s  .  co m
    try {
        org.hibernate.Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        tr = s.getTransaction();
        tr.begin();
        s.merge(ar);
        tr.commit();
        System.err.println("Se actualiz el movimiento correctamente ");
    } catch (HibernateException he) {
        if (tr != null && tr.isActive())
            tr.rollback();
        System.err.println("Sucedi un error");
        he.printStackTrace();
        return false;

    }
    return true;
}

From source file:Datacontroller.DataParser.java

public static boolean UpdateData(Object o) {
    try {/*w  w w  .  j  a  v a  2  s. c o  m*/
        Session s = Entities.HibernateUtil.getSessionFactory().openSession();
        Transaction tx = s.getTransaction();
        tx.begin();
        s.merge(o);
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}

From source file:de.appsolve.padelcampus.db.dao.generic.BaseEntityDAO.java

@Override
@SuppressWarnings("unchecked")
public T saveOrUpdate(T entity) {
    Session session = entityManager.unwrap(Session.class);
    if (entity.getId() == null) {
        Long id = (Long) session.save(entity);
        entity.setId(id);/*from   w  w  w .j  a va 2 s .  c om*/
    } else {
        entity = (T) session.merge(entity);
    }
    return entity;
}

From source file:de.csw.expertfinder.persistence.PersistenceStoreFacade.java

License:Open Source License

/**
 * Merges the given entity's properties with its equivalent (an entity with
 * the same identifier) stored in the current session and persists it. If no
 * such object exists in the session, it will be loaded from the persistence
 * layer./*ww w  . ja  v a 2 s. c om*/
 * 
 * @param entity
 * @return the merged entity.
 */
@SuppressWarnings("unchecked")
public PersistableEntity<? extends Serializable> merge(PersistableEntity<? extends Serializable> entity) {
    Session session = sessionFactory.getCurrentSession();
    return (PersistableEntity<? extends Serializable>) session.merge(entity);
}

From source file:de.fhdo.authorization.UsernamePasswordMethod.java

License:Apache License

public boolean resendPassword(String username) {
    boolean success = false;

    // Neues Passwort generieren
    String neuesPW = Password.generateRandomPassword(8);
    logger.debug("Neues Passwort: " + neuesPW);
    String mail = "";
    long userId = 0;
    Session hb_session = HibernateUtil.getSessionFactory().openSession();

    try {/*from w  w w  .j  a  va 2s .  co m*/
        //org.hibernate.Transaction tx = hb_session.beginTransaction();
        List list = hb_session.createQuery("from TermUser where name=:p_user").setString("p_user", username)
                .list();

        if (list.size() > 0) {
            TermUser user = (TermUser) list.get(0);
            userId = user.getId();
            logger.debug("userId: " + userId);

            // Email-Adresse lesen
            mail = user.getEmail();

            /*String salt = Password.generateRandomSalt();
            user.setPassw(Password.getSaltedPassword(neuesPW, salt, username, ITERATION_COUNT));
            user.setSalt(salt);
                    
            logger.debug("merge");
            hb_session.merge(user);
            logger.debug("commit");
            tx.commit();*/
            //logger.debug("commit fertig");
        }
    } catch (Exception e) {
        LoggingOutput.outputException(e, this);
    } finally {
        hb_session.close();
    }
    logger.debug("mail: " + mail);

    // Neues Passwort per Email versenden
    if (mail.length() > 0) {
        String result = Mail.sendNewPassword(username, neuesPW, mail);
        if (result.length() == 0)
            success = true;
        //String result = "";

        if (result.length() == 0) {
            logger.debug("change Password in db...");
            hb_session = HibernateUtil.getSessionFactory().openSession();

            try {
                org.hibernate.Transaction tx = hb_session.beginTransaction();

                TermUser user = (TermUser) hb_session.get(TermUser.class, userId);

                // Neues Passwort in der Datenbank speichern
                String salt = Password.generateRandomSalt();

                //logger.debug("getSaltedPassword: " + username + ", " + salt + ", " + neuesPW);
                user.setPassw(Password.getSaltedPassword(MD5.getMD5(neuesPW), salt, username, ITERATION_COUNT));
                user.setSalt(salt);

                hb_session.merge(user);
                tx.commit();
                success = true;
            } catch (Exception e) {
                LoggingOutput.outputException(e, this);
            } finally {
                hb_session.close();
            }
        }
    }
    return success;
}

From source file:de.fhdo.collaboration.db.DBSysParam.java

License:Apache License

/**
 Speichert einen Parameter in der Datenbank.
        /*www.j a  va 2s . c o  m*/
        
 @param Parameter der Parameter
 @return String mit Fehlermeldung oder leer bei Erfolg
 */
public String setValue(SysParam Parameter) {
    String ret = "";

    Session hb_session = HibernateUtil.getSessionFactory().openSession();
    hb_session.getTransaction().begin();

    try {
        applyDatatype(Parameter);

        hb_session.merge(Parameter);
    } catch (Exception ex) {
        ret = "Fehler bei 'setValue(): " + ex.getLocalizedMessage();
        ex.printStackTrace();
    }

    hb_session.getTransaction().commit();
    hb_session.close();

    return ret;
}

From source file:de.fhdo.collaboration.db.DomainHelper.java

License:Apache License

/**
 * Liest alle bergeordneten DomainValues zu einer DomainValue
 *
 *///w ww.java2s.com
public boolean saveUpperDomainID(long DomainValueID, long UpperID) {
    if (DomainValueID <= 0)
        return false;

    logger.debug("saveUpperDomainID mit '" + DomainValueID + "' und " + UpperID);

    DomainValue dv = null;
    //initDomain(DomainID);

    DomainValue ueber_dv = getDomainValue(UpperID);

    if (ueber_dv == null) {
        logger.debug("DomainValue mit ber-ID " + UpperID + " existiert nicht");
        return false;
    }

    Session hb_session = HibernateUtil.getSessionFactory().openSession();
    hb_session.getTransaction().begin();

    dv = (DomainValue) hb_session.get(DomainValue.class, DomainValueID);
    if (dv == null && logger.isDebugEnabled())
        logger.debug("DomainValue mit ID '" + DomainValueID + "' nicht gefunden!");

    //List<DomainValue> list = null;
    if (dv != null) {
        boolean gefunden = false;
        if (dv.getDomainValuesForDomainValueIdParent() != null) {
            Iterator<DomainValue> it = dv.getDomainValuesForDomainValueIdParent().iterator();
            while (it.hasNext()) {
                DomainValue ueber_dv2 = it.next();
                if (ueber_dv2.getId() == UpperID) {
                    logger.debug("ber-Domain bereits gefunden, also nicht hinzufgen");
                    gefunden = true;
                }
            }
        }

        if (gefunden == false) {
            logger.debug("ber-Domain hinzufgen...");

            if (dv.getDomainValuesForDomainValueIdParent() == null)
                dv.setDomainValuesForDomainValueIdParent(new HashSet<DomainValue>());

            dv.getDomainValuesForDomainValueIdParent().add(ueber_dv);

            hb_session.merge(dv);
        }
        //list = new LinkedList(dv.getDomainValuesForDomainValueId1());
    }

    // Transaktion abschlieen
    hb_session.getTransaction().commit();
    hb_session.close();

    return true;
}