Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

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

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:br.com.bean.RestControllers.turmaController.java

@RequestMapping(value = "insere-turma", method = RequestMethod.POST)
public String post(@RequestBody String turma, @RequestParam long faculdadeId) throws HibernateException {
    Gson gson = new Gson();
    Turma t = gson.fromJson(turma, Turma.class);
    Session sessao = HibernateUtility.getSession();
    Transaction transacao = sessao.beginTransaction();
    try {//w  w  w  . j  av  a  2  s  . c o  m
        Faculdade f = faculdadeController.buscaFaculdadeParaOperacao(faculdadeId);
        t.setFaculdade(f);
        f.getTurmas().add(t);
        t.setAtivo(1);
        sessao.saveOrUpdate(t);
        String jsonMensagemSucesso = CriadorJson.criaJsonSucesso("Dados Salvos");
        return jsonMensagemSucesso;
    } catch (ConstraintViolationException c) {
        transacao.rollback();
        String jsonMensagemErro = CriadorJson.criaJsonErro(c, "Registro Esta Sendo utilizado");
        return jsonMensagemErro;
    } catch (HibernateException e) {
        transacao.rollback();
        String jsonMensagemErro = CriadorJson.criaJsonErro(e, null);
        return jsonMensagemErro;
    } finally {
        transacao.commit();
        sessao.close();
    }
}

From source file:br.com.bean.Utilitarios.TestaHibernate.java

public static void incluiUsuario() throws ParseException {

    System.out.println("entrou");
    Session sessao = HibernateUtility.getSession();
    Transaction transacao = sessao.beginTransaction();

    Usuario u = new Usuario(1);
    u.setCelular("95476-9088");
    u.setCep("09991100");
    u.setCidade("Diadema");
    u.setComplemento("Teste");
    u.setCpf("44385902801");
    u.setDataNascimento("27/12/1994");
    u.setEmail("guidelmiglio@hotmail.com");
    u.setSenha("123");
    u.setEndereco("rua tukanos n57");
    u.setEstado("So Paulo");
    u.setNome("delmiglio");
    u.setRg("487395207");
    u.setTelefone("9080-9080");

    sessao.saveOrUpdate(u);

    transacao.commit();/*from w ww  .ja v a2 s  .c  o m*/
    sessao.close();

}

From source file:br.com.ln.dao.GenericDao.java

/**
* Salvar ou Atualizar o Objeto//w w  w . j  a va 2s  .c om
* @param obj save or update a obj
* @param strDbName
*
*/
public static void saveOrUpdateObject(Object obj) {
    Session session = null;
    Transaction tx;
    try {
        session = SessionFactoryDbName.getCurrentSessionByName(VarComuns.strDbName);
        tx = session.beginTransaction();
        session.saveOrUpdate(obj);
        tx.commit();
    } catch (HibernateException ex) {
        logger.error("Problemas na gravao : " + ex.getMessage());
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
}

From source file:br.com.ln.hibernate.Postgress.java

/**
 *
 * @param obj save or update a obj/*from  w ww . j  a v a 2  s . c  o m*/
 * @param strDbName
 *
 */
public static void saveOrUpdateObject(Object obj) {
    Session session = null;
    Transaction tx;
    try {
        session = SessionFactoryDbName.getCurrentSessionByName(VarComuns.strDbName);
        tx = session.beginTransaction();
        session.saveOrUpdate(obj);
        tx.commit();
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
}

From source file:br.com.moises.dao.Dao.java

@Override
public boolean save(T entity) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {/*from  w  w  w . j  ava2 s  . c  o m*/

        session.getTransaction().begin();
        session.saveOrUpdate(entity);
        session.getTransaction().commit();
        FacesUtil.addInfoMessage("Salvo com sucesso!!");
        return true;
    } catch (Exception e) {
        session.getTransaction().rollback();
        FacesUtil.addInfoMessage("Erro ao salvar!!!" + e);
        return false;
    }

}

From source file:br.com.moises.dao.Dao.java

@Override
public boolean saveOrUpdate(T entity) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {//w  ww  . j av  a2  s  .  co  m

        session.getTransaction().begin();
        session.saveOrUpdate(entity);
        session.getTransaction().commit();
        FacesUtil.addInfoMessage("Salvo com sucesso!!");
        return true;
    } catch (Exception e) {
        session.getTransaction().rollback();
        FacesUtil.addInfoMessage("Erro ao salvar!!!" + e);
        return false;
    }
}

From source file:br.com.pucminas.debt.dao.impl.ProjetoDAOImpl.java

@Override
public void salvar(Projeto projeto) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = null;//w  w w.jav  a  2  s . co m
    try {
        t = session.beginTransaction();
        session.saveOrUpdate(projeto);
        session.getTransaction().commit();
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Projeto " + projeto.getNome(), "Cadastrado com sucesso!"));
    } catch (HibernateException e) {
        if (t != null) {
            t.rollback();
        }
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro!",
                "No foi possvel cadastrar o projeto: " + e));
    } finally {
        session.close();
    }
}

From source file:br.com.sinax.musicpoc.dao.MusicDaoImpl.java

License:Apache License

@Override
public Music persist(Music entity) {

    Session session = sessionProvider.provide();
    session.beginTransaction();/* ww  w.j a  v  a2 s  .  com*/
    session.saveOrUpdate(entity);
    session.getTransaction().commit();
    if (session.isOpen()) {

        session.close();
    }
    return entity;
}

From source file:br.com.ufmt.fata.dao.ComplementoDaoImp.java

@Override
public void save(Complemento complemento) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    session.saveOrUpdate(complemento);
    t.commit();//from w ww.j a  va2  s .co  m
    session.close();
}

From source file:br.com.ufmt.fata.dao.PastaDeComunicacaoDaoImp.java

@Override
public void save(PastaDeComunicacao pastaDeComunicacao) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction t = session.beginTransaction();
    session.saveOrUpdate(pastaDeComunicacao);
    t.commit();/*w w w . ja va  2 s .c o  m*/
    session.close();
}