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:model.PessoajuridicaModel.java

public String update(Pessoajuridica obj) {
    //Abre um sesso
    Session objSession = this.objSessionFactory.openSession();
    //Inicia uma transao dentro da sesso aberta
    Transaction objTransaction = objSession.beginTransaction();

    try {/*from   w  ww.  j  av  a  2  s.co m*/
        //ATUALIZA o objeto categoria, assim o hibernate persiste no bancoapagando o registro.
        objSession.merge(obj);
        //Realiza um commit do UPDATE
        objTransaction.commit();
    } catch (Exception e) {
        //Caso ocorrer algum erro, mostra uma mensagem
        msg = e.getMessage();
        //Realiza o Rollback, cancelando o UPDATE no banco de dados.
        objTransaction.rollback();
    }

    //Fecha a sesso
    objSession.close();
    //Retorna a mensagem
    return msg;
}

From source file:model.PessoaModel.java

public String update(Pessoa obj) {
    //Abre um sesso
    Session objSession = this.objSessionFactory.openSession();
    //Inicia uma transao dentro da sesso aberta
    Transaction objTransaction = objSession.beginTransaction();

    try {//from w  ww  . ja  v a 2 s .  c  o  m
        //ATUALIZA o objeto categoria, assim o hibernate persiste no bancoapagando o registro.
        objSession.merge(obj);
        //Realiza um commit do UPDATE
        objTransaction.commit();
    } catch (Exception e) {
        //Caso ocorrer algum erro, mostra uma mensagem
        msg = e.getMessage();
        //Realiza o Rollback, cancelando o UPDATE no banco de dados.
        objTransaction.rollback();
    }

    //Fecha a sesso
    objSession.close();
    //Retorna a mensagem
    return msg;
}

From source file:model.TipoatendimentoModel.java

public String update(Tipoatendimento obj) {
    //Abre um sesso
    Session objSession = this.objSessionFactory.openSession();
    //Inicia uma transao dentro da sesso aberta
    Transaction objTransaction = objSession.beginTransaction();

    try {/*  w w  w .  ja v a  2 s  .  c  om*/
        //ATUALIZA o objeto categoria, assim o hibernate persiste no bancoapagando o registro.
        objSession.merge(obj);
        //Realiza um commit do UPDATE
        objTransaction.commit();
    } catch (Exception e) {
        //Caso ocorrer algum erro, mostra uma mensagem
        msg = e.getMessage();
        //Realiza o Rollback, cancelando o UPDATE no banco de dados.
        objTransaction.rollback();
    }

    //Fecha a sesso
    objSession.close();
    //Retorna a mensagem
    return msg;
}

From source file:monasca.thresh.infrastructure.persistence.hibernate.AlarmSqlImpl.java

License:Apache License

private MetricDefinitionDimensionsDb insertMetricDefinitionDimension(final Session session,
        final MetricDefinitionAndTenantId mdtId) {
    final MetricDefinitionDb metricDefinition = this.insertMetricDefinition(session, mdtId);
    final BinaryId metricDimensionSetId = this.insertMetricDimensionSet(session,
            mdtId.metricDefinition.dimensions);
    final byte[] definitionDimensionsIdSha1Hash = DigestUtils
            .sha(metricDefinition.getId().toHexString() + metricDimensionSetId.toHexString());
    final MetricDefinitionDimensionsDb metricDefinitionDimensions = new MetricDefinitionDimensionsDb(
            definitionDimensionsIdSha1Hash, metricDefinition, metricDimensionSetId);
    return (MetricDefinitionDimensionsDb) session.merge(metricDefinitionDimensions);
}

From source file:monasca.thresh.infrastructure.persistence.hibernate.AlarmSqlImpl.java

License:Apache License

private BinaryId insertMetricDimensionSet(Session session, Map<String, String> dimensions) {
    final byte[] dimensionSetId = calculateDimensionSHA1(dimensions);
    for (final Map.Entry<String, String> entry : dimensions.entrySet()) {

        final MetricDimensionDb metricDimension = new MetricDimensionDb(dimensionSetId, entry.getKey(),
                entry.getValue());/*  ww w . j a v  a  2s  .c o  m*/

        if (session.get(MetricDimensionDb.class, metricDimension.getId()) == null) {
            session.merge(metricDimension);
        }

    }

    return new BinaryId(dimensionSetId);
}

From source file:monasca.thresh.infrastructure.persistence.hibernate.AlarmSqlImpl.java

License:Apache License

private MetricDefinitionDb insertMetricDefinition(final Session session,
        final MetricDefinitionAndTenantId mdtid) {

    final String region = ""; // TODO We currently don't have region
    final String definitionIdStringToHash = truncateString(mdtid.metricDefinition.name, MAX_COLUMN_LENGTH)
            + truncateString(mdtid.tenantId, MAX_COLUMN_LENGTH) + truncateString(region, MAX_COLUMN_LENGTH);
    final byte[] id = DigestUtils.sha(definitionIdStringToHash);
    final MetricDefinitionDb metricDefinition = new MetricDefinitionDb(id, mdtid.metricDefinition.name,
            mdtid.tenantId, region);//w w w . j  a v  a2s  .  c  o m

    if (session.get(MetricDefinitionDb.class, metricDefinition.getId()) == null) {
        session.persist(metricDefinition);
        return metricDefinition;
    }

    session.merge(metricDefinition);
    return metricDefinition;
}

From source file:MVCHibernateDB.Model.java

static void updateUser(DataBaseUser updateDB) {
    Session session = DataBaseSF.getSessionFactory().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    Query userDB = (Query) session.createQuery("Select u from DataBaseUser as u where u.id = :uId");
    session.merge(userDB.setParameter("uId", updateDB.getUserId()));
    transaction.commit();//from   w  w w.j  a  v  a2s . co m
}

From source file:mx.edu.um.mateo.colportor.dao.AsociacionDao.java

License:Open Source License

public Asociacion actualiza(Asociacion asociacion, Usuario usuario) {
    Session session = currentSession();
    if (usuario != null) {
        asociacion.setUnion(usuario.getAsociacion().getUnion());
    }//from  w  ww  .  java  2 s.  c o m
    try {
        // Actualiza la asociacion
        log.debug("Actualizando asociacion");
        session.update(asociacion);
        session.flush();

    } catch (NonUniqueObjectException e) {
        try {
            session.merge(asociacion);
        } catch (Exception ex) {
            log.error("No se pudo actualizar la asociacion", ex);
            throw new RuntimeException("No se pudo actualizar la asociacion", ex);
        }
    }
    if (usuario != null) {
        usuario.setAsociacion(asociacion);
    }
    session.flush();
    return asociacion;
}

From source file:mx.edu.um.mateo.colportor.dao.TipoColportorDao.java

License:Open Source License

public TipoColportor actualiza(TipoColportor tipoColportor, Usuario usuario) {
    Session session = currentSession();

    try {/*  w  ww .  j ava2s  .  c  o m*/
        // Actualiza la tipoColportor
        log.debug("Actualizando tipoColportor");
        session.update(tipoColportor);
        session.flush();

    } catch (NonUniqueObjectException e) {
        try {
            session.merge(tipoColportor);
        } catch (Exception ex) {
            log.error("No se pudo actualizar la tipoColportor", ex);
            throw new RuntimeException("No se pudo actualizar la tipoColportor", ex);
        }
    }

    session.flush();
    return tipoColportor;
}

From source file:mx.edu.um.mateo.colportor.dao.UnionDao.java

License:Open Source License

public Union actualiza(Union union, Usuario usuario) {
    Session session = currentSession();
    log.debug("NombreCompleto: {}", union.getNombre());
    session.update(union);/* w  w w. ja va 2s.  c o m*/
    session.merge(union);
    session.flush();
    if (usuario != null) {
        session.refresh(union);
        actualizaUsuario: for (Asociacion asociacion : union.getAsociaciones()) {
            usuario.setAsociacion(asociacion);
            session.update(usuario);
            break actualizaUsuario;
        }
    }

    session.flush();
    return union;
}