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:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.sql.SqlAuthTokenPersister.java

License:Open Source License

/**
 * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.AuthTokenPersister#
 *      persistAuthToken(com.google.api.ads.adwords.jaxws.extensions.report.model.entities.AuthMcc)
 *//* ww  w  .  j av a 2 s .c o m*/
@Override
@Transactional
public void persistAuthToken(AuthMcc authToken) {

    Session session = this.sessionFactory.getCurrentSession();
    session.saveOrUpdate(authToken);
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.sql.SqlReportEntitiesPersister.java

License:Open Source License

/**
 * Persists all the given entities into the DB configured in the {@code SessionFactory}
 *//* w  w  w.j  a va2 s .c om*/
@Override
@Transactional
public void persistReportEntities(List<? extends Report> reportEntities) {

    int batchFlush = 0;

    Session session = this.sessionFactory.getCurrentSession();

    for (Report report : reportEntities) {
        report.setId();
        session.saveOrUpdate(report);
        batchFlush++;

        if (batchFlush == BATCH_SIZE) {
            session.flush();
            session.clear();
        }
    }
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.sql.SqlReportEntitiesPersister.java

License:Open Source License

/**
 * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.EntityPersister
 *      #save(com.google.api.ads.adwords.jaxws.extensions.report.model.entities.Report)
 *//*w w  w .  j  a  v a  2  s  .c o m*/
@Override
@Transactional
public <T> T save(T t) {

    Session session = this.sessionFactory.getCurrentSession();
    session.saveOrUpdate(t);
    return t;
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.sql.SqlReportEntitiesPersister.java

License:Open Source License

/**
 * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.EntityPersister
 *      #save(java.util.List)//  ww  w. jav a 2  s . c  o m
 */
@Override
@Transactional
public <T> void save(List<T> reports) {

    Session session = this.sessionFactory.getCurrentSession();
    for (T report : reports) {
        session.saveOrUpdate(report);
    }
}

From source file:com.hasz.dao.ClienteFisicoDAO.java

public static void cadastrarClienteFisico(ClienteFisico cf) {
    Session sessao = HibernateUtil.getSession();
    Transaction t = sessao.beginTransaction();
    try {//  ww  w .java 2  s.  c o  m
        cf.setDataCadastro(new Date());
        sessao.saveOrUpdate(cf);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        t.commit();
        sessao.close();
    }
}

From source file:com.hasz.dao.ClienteJuridicoDAO.java

public static void cadastrarClienteJuridico(ClienteJuridico cj) {
    Session sessao = HibernateUtil.getSession();
    Transaction t = sessao.beginTransaction();
    try {//from  ww  w . j a v a 2 s.  com
        cj.setDataCadastro(new Date());
        sessao.saveOrUpdate(cj);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        t.commit();
        sessao.close();
    }
}

From source file:com.hasz.dao.EstadoDAO.java

public static boolean cadastrarEstado(Estado estado) {
    Session sessao = HibernateUtil.getSession();
    Transaction t = sessao.beginTransaction();
    boolean retorno;
    try {// w ww .j  a va2 s . c o m
        List<Cidade> cidades = new ArrayList<Cidade>(estado.getCidade());
        Iterator it = cidades.iterator();
        while (it.hasNext()) {
            sessao.saveOrUpdate((Cidade) it.next());
        }
        sessao.saveOrUpdate(estado);
        retorno = true;
    } catch (Exception e) {
        e.printStackTrace();
        retorno = false;
    } finally {
        t.commit();
        sessao.close();
    }
    return retorno;
}

From source file:com.hasz.dao.EstadoDAO.java

public static boolean editarEstado(Estado estado) {
    Session sessao = HibernateUtil.getSession();
    Transaction t = sessao.beginTransaction();
    boolean retorno;
    try {/*from ww w.j a v  a2 s .  c  om*/
        List<Cidade> cidades = new ArrayList<Cidade>(estado.getCidade());
        Iterator it = cidades.iterator();
        while (it.hasNext()) {
            sessao.saveOrUpdate((Cidade) it.next());
        }
        sessao.saveOrUpdate(estado);
        retorno = true;
    } catch (Exception e) {
        e.printStackTrace();
        retorno = false;
    } finally {
        t.commit();
        sessao.close();
    }
    return retorno;
}

From source file:com.hasz.dao.EstadoDAO.java

public static boolean deletarEstado(Estado estado) {
    Session sessao = HibernateUtil.getSession();
    Transaction t = sessao.beginTransaction();
    boolean retorno;
    try {/*  ww w  .  j  a  v  a2 s . co m*/
        List<Cidade> cidades = new ArrayList<Cidade>(estado.getCidade());
        Iterator it = cidades.iterator();
        while (it.hasNext()) {
            sessao.saveOrUpdate((Cidade) it.next());
        }
        sessao.saveOrUpdate(estado);
        retorno = true;
    } catch (Exception e) {
        e.printStackTrace();
        retorno = false;
    } finally {
        t.commit();
        sessao.close();
    }
    return retorno;
}

From source file:com.hasz.dao.ReservaDAO.java

public static void cadastrarReserva(Reserva r, List<Servico> s) {
    Session sessao = HibernateUtil.getSession();
    Transaction t = sessao.beginTransaction();
    try {//  w  w  w . java2  s . c om
        r.setData(new Date());
        r.setCliente((Cliente) sessao.get(Cliente.class, r.getCliente().getIdCliente()));
        r.setHotel((Hotel) sessao.get(Hotel.class, r.getHotel().getIdHotel()));
        r.setQuarto((Quarto) sessao.get(Quarto.class, r.getQuarto().getIdQuarto()));

        for (int n = 0; n < s.size(); n++) {

            ReservaServico rs = new ReservaServico();
            Servico sTemp = s.get(n);
            rs.setData(new Date());
            //r.getReservaservico().add(rs);
            //s.get(n).getReservaservico().add(rs);

            //                ReservaServico rs = new ReservaServico();
            //                ReservaServicoId rsID = new ReservaServicoId();
            //                Servico sAux = s.get(n);
            //                rsID.setReserva(r);
            //                rsID.setServico(sAux);
            //                rs.setReservaServicoId(rsID);
            //                rs.setData(new Date());
            //                sessao.saveOrUpdate(rs);
            sessao.saveOrUpdate(rs);
        }
        sessao.saveOrUpdate(r);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        t.commit();
        sessao.close();
    }
}