Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

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

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

From source file:br.com.bluesoft.pronto.dao.HibernateAuditLogListener.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  w ww.  j a  va 2 s . c  o  m*/
public final boolean onPreUpdate(final PreUpdateEvent event) {

    Session session = null;

    try {

        if (!event.getEntity().getClass().equals(Ticket.class)) {
            return false;
        }

        final Serializable entityId = event.getPersister().hasIdentifierProperty() ? event.getPersister()
                .getIdentifier(event.getEntity(), event.getPersister().guessEntityMode(event.getEntity()))
                : null;
        final Date transTime = new Date();
        final EntityMode entityMode = event.getPersister().guessEntityMode(event.getEntity());
        Object oldPropValue = null;
        Object newPropValue = null;

        session = event.getPersister().getFactory().openSession();
        final Transaction tx = session.beginTransaction();

        final Object existingEntity = session.get(event.getEntity().getClass(), entityId);

        if (existingEntity != null) {

            for (final String propertyName : event.getPersister().getPropertyNames()) {

                String campo = propertyName;
                final boolean temLabel = Ticket.class.getDeclaredField(propertyName)
                        .isAnnotationPresent(Label.class);
                if (temLabel) {
                    campo = Ticket.class.getDeclaredField(propertyName).getAnnotation(Label.class).value();
                }

                newPropValue = event.getPersister().getPropertyValue(event.getEntity(), propertyName,
                        entityMode);
                if (newPropValue != null) {
                    final boolean ehUmaCollection = newPropValue instanceof Collection;
                    if (!ehUmaCollection) {
                        oldPropValue = event.getPersister().getPropertyValue(existingEntity, propertyName,
                                entityMode);

                        final String oldValue = makeString(oldPropValue);
                        final String newValue = makeString(newPropValue);

                        if (!oldValue.equals(newValue)) {
                            final TicketLog history = new TicketLog();
                            history.setTicket((Ticket) event.getEntity());
                            history.setData(transTime);
                            history.setUsuario(Seguranca.getUsuario().getUsername());
                            history.setCampo(campo);
                            history.setValorAntigo(oldValue);
                            history.setValorNovo(newValue);
                            history.setOperacao(TicketLog.ALTERACAO);
                            if (history.isDiferente()) {
                                session.save(history);
                            }
                        }
                    }
                }
            }
        }

        tx.commit();
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return false;
}

From source file:br.com.caelum.vraptor.util.migration.HibernateConnectionProvider.java

License:Open Source License

private void execute(Session session, Migration<Session> migration) {
    migration.execute(session);/*from   ww w  .j  ava 2 s.  c o  m*/
    session.save(new MigrationInfo(migration.getId()));
}

From source file:br.com.chamado.dao.DaoGenerico.java

public void salvar(Object obj) throws HibernateException {

    Session session = hibernateConfiguracao.openSession();
    Transaction transaction = session.beginTransaction();
    session.save(obj);
    transaction.commit();/*  w  w w. j  a v  a2 s . c  o  m*/
    session.close();

}

From source file:br.com.clinica.dao.ConsultaDao.java

public void salvar(Consulta consulta) {
    Session ss = HibernateUtil.getSessionFactory().getCurrentSession();
    ss.beginTransaction();/*ww  w  . ja va2  s . c o m*/
    ss.save(consulta);
    ss.getTransaction().commit();
    //consulta = new Consulta();
    //ss.close();

}

From source file:br.com.clinica.dao.DadosAdicionaisDao.java

@Override
public void salvar(DadosAdicionais dadosAdicionais) {
    Session ss = HibernateUtil.getSessionFactory().getCurrentSession();
    ss.beginTransaction();/*ww w  .ja  v  a2  s  .c o  m*/
    ss.save(dadosAdicionais);
    ss.getTransaction().commit();

}

From source file:br.com.clinica.dao.PessoaDao.java

@Override
public void salvar(Pessoa pessoa) {
    Session ss = HibernateUtil.getSessionFactory().getCurrentSession();

    try {//w  w w. j  a v a 2s.c o  m
        ss.beginTransaction();
        try {
            ss.save(pessoa);
            ss.getTransaction().commit();
        } catch (Exception ex) {
            // Log the exception here
            ss.getTransaction().rollback();
            throw ex;
        }
    } finally {
        HibernateUtil.getSessionFactory().getCurrentSession().close();

    }
}

From source file:br.com.clinica.dao.ProntuarioDao.java

@Override
public void salvar(Prontuario prontuario) {
    Session ss = HibernateUtil.getSessionFactory().getCurrentSession();

    try {//from w ww  .  j av  a  2  s. c om
        ss.beginTransaction();
        try {
            ss.save(prontuario);
            ss.getTransaction().commit();
        } catch (Exception ex) {
            // Log the exception here
            ss.getTransaction().rollback();
            throw ex;
        }
    } finally {
        HibernateUtil.getSessionFactory().getCurrentSession().close();
    }
}

From source file:br.com.Dao.HibernateDao.java

@Override
public void save(T entity) {
    Session.save(entity);
}

From source file:br.com.dao.UsuarioImp.java

public void save(Usuario usuario) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    session.save(usuario);
    t.commit();//w w  w.j av a2  s . co  m
}

From source file:br.com.fabiose.book.persistence.BookPersistence.java

public BookModel add(BookModel bookModel, Session session) throws Exception {
    Transaction t = null;//from w w  w. j  a va2s  .  c  o m
    try {
        t = session.beginTransaction();
        session.save(bookModel);
        t.commit();
        return bookModel;
    } catch (Exception e) {
        t.rollback();
        throw new Exception(e.getMessage());
    }
}