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:es.emergya.bbdd.dao.HistoricoGPSHome.java

License:Open Source License

@Transactional(readOnly = false, rollbackFor = Throwable.class, propagation = Propagation.REQUIRES_NEW)
public HistoricoGPS saveOrUpdate(HistoricoGPS hgps) {

    Session currentSession = getSession();
    currentSession.clear();//from   w  ww . ja v a 2s .  co  m

    HistoricoGPS entity = null;

    if (hgps.getId() == null || (hgps.getId() != null && this.get(hgps.getId()) == null))
        entity = hgps;
    else
        entity = (HistoricoGPS) currentSession.merge(hgps);

    currentSession.saveOrUpdate(entity);
    return entity;
}

From source file:es.emergya.bbdd.dao.PatrullaHome.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = Throwable.class)
public boolean saveOrUpdate(Patrulla p) {
    boolean res = false;
    if (p == null)
        return res;
    log.debug("Saving " + p);
    try {/*ww w .ja  va2 s.  co  m*/
        Session currentSession = getSession();
        currentSession.clear();

        Patrulla entity = null;

        if (p.getId() != null && this.get(p.getId()) != null)
            entity = (Patrulla) currentSession.merge(p);
        else
            entity = p;

        if (p.getRecursos() != null)
            for (Recurso r : p.getRecursos()) {
                if (r.getId() != null) {
                    r = (Recurso) currentSession.get(Recurso.class, r.getId());
                    r.setPatrullas(entity);
                    currentSession.saveOrUpdate(r);
                }
            }

        currentSession.saveOrUpdate(entity);

        log.debug(p + " saved");
        return true;

    } catch (Throwable t) {
        log.error(t, t);
        return false;
    }
}

From source file:es.emergya.bbdd.dao.RecursoHome.java

License:Open Source License

@Transactional(readOnly = false, rollbackFor = Throwable.class, propagation = Propagation.REQUIRES_NEW)
public boolean saveOrUpdate(Recurso p) {
    boolean res = false;
    if (p == null) {
        return res;
    }/*  ww w.j av  a2 s .co m*/
    try {
        Session currentSession = getSession();
        currentSession.clear();

        Recurso entity = null;

        if (p.getId() == null || (p.getId() != null && this.get(p.getId()) == null)) {
            entity = p;
        } else {
            entity = (Recurso) currentSession.merge(p);
            if (p.getEstadoEurocop() != null && p.getEstadoEurocop().getId() != null) {
                entity.setEstadoEurocop(
                        (EstadoRecurso) currentSession.get(EstadoRecurso.class, p.getEstadoEurocop().getId()));
            }
        }
        if (entity == null) {
            entity = p;
        }

        if (entity != null) {
            entity.setInfoAdicional(p.getInfoAdicional());
            entity.setNombre(p.getNombre());
        }

        Patrulla patrulla = null;
        if (p.getPatrullas() != null) {
            if (p.getPatrullas().getId() != null) {
                patrulla = (Patrulla) currentSession.load(Patrulla.class, p.getPatrullas().getId());
            } else {
                patrulla = p.getPatrullas();
            }
        }
        entity.setPatrullas(patrulla);

        if (entity.getEstadoEurocop() == null) {
            entity.setEstadoEurocop((EstadoRecurso) currentSession.get(EstadoRecurso.class, 1l));
        }

        entity.setIdentificador(entity.getNombre());
        currentSession.saveOrUpdate(entity);
    } catch (Throwable t) {
        log.error(t, t);
    }
    return true;
}

From source file:es.emergya.bbdd.dao.UsuarioHome.java

License:Open Source License

@Transactional(readOnly = false, rollbackFor = Throwable.class, propagation = Propagation.REQUIRES_NEW)
public boolean saveOrUpdate(Usuario entity) {
    log.trace("saveOrUpdate(" + entity + ")");
    if (entity == null)
        throw new NullPointerException("No se puede guardar un usuario nulo");
    org.hibernate.Session currentSession = getSession();
    currentSession.clear();//from  www.  ja  v  a  2s  .co  m
    Object e = null;
    try {
        if (entity.getId() == null || (entity.getId() != null && this.get(entity.getId()) == null)) {
            log.trace("Tenemos que crear un usuario nuevo");
            e = entity;
        } else {
            log.trace("Hacemos update sobre un usuario ya antiguo");
            e = currentSession.merge(entity);
        }
    } catch (Throwable t) {
        log.error("Tiene toda la pinta de que estamos guardando algo ya borrado", t);
    }
    if (e == null) {
        log.debug("Error al mergear");
        throw new NullPointerException("No se puede guardar el usuario, es nulo");
    }
    currentSession.saveOrUpdate(e);
    log.trace("saved");
    return true;
}

From source file:es.sm2.openppm.core.logic.impl.BusinessdriverLogic.java

License:Open Source License

/**
 * Save Business Driver/*from  w  ww . j  av a2  s.  co m*/
 * @param businessdriver
 * @return
 * @throws Exception 
 */
public Businessdriver saveBusinessDriver(Businessdriver businessdriver) throws Exception {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
        tx = session.beginTransaction();
        BusinessdriverDAO businessdriverDAO = new BusinessdriverDAO(session);

        businessdriver = (Businessdriver) session.merge(businessdriver);

        if (businessdriverDAO.getTotalPriority(businessdriver.getBusinessdriverset()) > 100) {
            throw new LogicException("msg.error.business_driver_set.priority");
        }

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        SessionFactoryUtil.getInstance().close();
    }
    return businessdriver;
}

From source file:es.sm2.openppm.core.logic.impl.RiskRegisterLogic.java

License:Open Source License

/**
 * Save project risk/*from  www.jav  a2s .  com*/
 * @param kpi
 * @param user 
 * @param saveReassessmentLog 
 * @throws Exception 
 */
public Riskregister saveProjectRisk(Riskregister risk, Employee user, ResourceBundle idioma,
        boolean saveReassessmentLog) throws Exception {
    Transaction tx = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
        tx = session.beginTransaction();

        RiskRegisterDAO riskRegisterDAO = new RiskRegisterDAO(session);
        HistoricriskDAO historicriskDAO = new HistoricriskDAO(session);

        // If update
        if (risk.getIdRisk() != null) {
            // Save risk reassessments
            if (saveReassessmentLog == true) {
                RiskReassessmentLogDAO riskReassessmentLogDAO = new RiskReassessmentLogDAO(session);

                Riskregister riskPrevious = riskRegisterDAO.findById(risk.getIdRisk());

                List<String> changes = changesInRisk(riskPrevious, risk, idioma, session);

                for (String change : changes) {
                    Riskreassessmentlog riskreassessmentlog = new Riskreassessmentlog();

                    riskreassessmentlog.setRiskregister(risk);
                    riskreassessmentlog.setRiskDate(new Date());
                    riskreassessmentlog.setRiskChange(change);
                    riskreassessmentlog.setEmployee(user);

                    riskReassessmentLogDAO.makePersistent(riskreassessmentlog);
                }
            }
        }

        // Save risk
        risk = (Riskregister) session.merge(risk);

        // Save historic
        Historicrisk historicriskLastDate = historicriskDAO.findbyIdRiskAndLastDate(risk);

        if (historicriskLastDate == null
                || (!historicriskLastDate.getProbability().equals(risk.getProbability())
                        || !historicriskLastDate.getImpact().equals(risk.getImpact()))) {
            Historicrisk historicrisk = new Historicrisk();

            historicrisk.setRiskregister(risk);
            historicrisk.setProbability(risk.getProbability());
            historicrisk.setImpact(risk.getImpact());
            historicrisk.setEmployee(user);
            historicrisk.setActualDate(new Date());

            historicriskDAO.makePersistent(historicrisk);
        }

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        SessionFactoryUtil.getInstance().close();
    }
    return risk;
}

From source file:eu.gruchala.crud.model.ProductsDao.java

@Override
public void update(Product product) {
    final Session currentSession = getCurrentSession();
    final Product oldProduct = getByHash(product.getHash(), currentSession);
    product.setId(oldProduct.getId());// w ww. j  a v a  2s . c  om
    currentSession.merge(product);
}

From source file:eu.interedition.text.Annotation.java

License:Apache License

public static Iterable<Annotation> create(Session session, Iterable<Annotation> annotations) {
    final Map<Name, Name> resolvedNames = Maps.newHashMap();
    for (Name name : Name.get(session, Sets.newHashSet(Iterables.transform(annotations, Annotation.NAME)))) {
        resolvedNames.put(name, name);/*  w w  w  . j  a v a2s .  c  o  m*/
    }

    final List<Annotation> created = Lists.newArrayList();
    for (Annotation annotation : annotations) {
        annotation.setName(Preconditions.checkNotNull(resolvedNames.get(annotation.getName())));
        created.add((Annotation) session.merge(annotation));
    }
    return created;
}

From source file:eu.interedition.text.Name.java

License:Apache License

public static Set<Name> get(Session session, Set<Name> names) {
    final Set<Name> result = Sets.newHashSet();
    if (names.isEmpty()) {
        return result;
    }//  w  w w.j  av  a2 s  .c o m

    final Set<Name> toFind = Sets.newHashSet(names);
    final Disjunction or = Restrictions.disjunction();
    for (Name name : toFind) {
        final Conjunction and = Restrictions.conjunction();
        and.add(Restrictions.eq("localName", name.getLocalName()));

        final URI ns = name.getNamespace();
        and.add(ns == null ? Restrictions.isNull("namespaceURI")
                : Restrictions.eq("namespaceURI", ns.toString()));

        or.add(and);
    }

    for (Name name : SQL.iterate(session.createCriteria(Name.class).add(or), Name.class)) {
        toFind.remove(name);
        result.add(name);
    }
    for (Name name : toFind) {
        result.add((Name) session.merge(name));
    }
    return result;
}

From source file:eu.interedition.text.Text.java

License:Apache License

public Text write(Session session, Reader contents, long contentLength) throws IOException {
    Text text = (Text) session.merge(this);

    final TextDigestingFilterReader digestingFilterReader = new TextDigestingFilterReader(
            new BufferedReader(contents));
    text.setLength(contentLength);//from w  w  w . j a  v a 2  s.  c  o  m
    text.setContent(Hibernate.getLobCreator(session).createClob(digestingFilterReader, contentLength));

    session.flush();
    session.refresh(text);

    text.setDigest(digestingFilterReader.digest());

    return text;
}