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:mx.edu.um.mateo.contabilidad.dao.impl.EjercicioDaoHibernate.java

License:Open Source License

@Override
public Ejercicio actualiza(Ejercicio ejercicio, Usuario usuario) {
    Session session = currentSession();
    if (usuario != null) {
        ejercicio.getId().setOrganizacion(usuario.getEmpresa().getOrganizacion());
    }//w  w w.j  a  va 2s  .c o  m
    try {
        // Actualiza la ejercicio
        log.debug("Actualizando ejercicio");
        session.update(ejercicio);
        session.flush();
    } catch (NonUniqueObjectException e) {
        try {
            session.merge(ejercicio);
            session.flush();
        } catch (Exception ex) {
            log.error("No se pudo actualizar la ejercicio", ex);
            throw new RuntimeException("No se pudo actualizar la ejercicio", ex);
        }
    }
    return ejercicio;
}

From source file:mx.edu.um.mateo.general.dao.impl.EmpresaDaoHibernate.java

License:Open Source License

@Override
public Empresa actualiza(Empresa empresa, Usuario usuario) {
    Session session = currentSession();
    if (usuario != null) {
        empresa.setOrganizacion(usuario.getEmpresa().getOrganizacion());
    }/* ww w. j a  va  2 s.  c  o m*/
    try {
        // Actualiza la empresa
        log.debug("Actualizando empresa");
        session.update(empresa);
        session.flush();

        // Actualiza proveedor
        log.debug("Actualizando proveedor");
        Query query = session
                .createQuery("select p from Proveedor p where p.empresa.id = :empresaId and p.base is true");
        query.setLong("empresaId", empresa.getId());
        Proveedor proveedor = (Proveedor) query.uniqueResult();
        log.debug("{}", proveedor);
        proveedor.setNombre(empresa.getNombre());
        proveedor.setNombreCompleto(empresa.getNombreCompleto());
        proveedor.setRfc(empresa.getRfc());
        session.update(proveedor);

        // Actualiza cliente
        log.debug("Actualizando cliente");
        query = session
                .createQuery("select c from Cliente c where c.empresa.id = :empresaId and c.base is true");
        query.setLong("empresaId", empresa.getId());
        Cliente cliente = (Cliente) query.uniqueResult();
        cliente.setNombre(empresa.getNombre());
        cliente.setNombreCompleto(empresa.getNombreCompleto());
        cliente.setRfc(empresa.getRfc());
        session.update(cliente);
    } catch (NonUniqueObjectException e) {
        try {
            session.merge(empresa);
        } catch (Exception ex) {
            log.error("No se pudo actualizar la empresa", ex);
            throw new RuntimeException("No se pudo actualizar la empresa", ex);
        }
    }
    if (usuario != null) {
        actualizaUsuario: for (Almacen almacen : empresa.getAlmacenes()) {
            usuario.setEmpresa(empresa);
            usuario.setAlmacen(almacen);
            session.update(usuario);
            break actualizaUsuario;
        }
    }
    session.flush();
    return empresa;
}

From source file:mx.edu.um.mateo.inventario.dao.impl.AlmacenDaoHibernate.java

License:Open Source License

@Override
public Almacen actualiza(Almacen almacen, Usuario usuario) {
    Session session = currentSession();
    if (usuario != null) {
        almacen.setEmpresa(usuario.getEmpresa());
    }/*from   ww w  .  j av  a  2s .co  m*/
    try {
        // Actualiza la almacen
        log.debug("Actualizando almacen");
        session.update(almacen);
        session.flush();

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

From source file:mx.edu.um.mateo.rh.dao.impl.PuestoDaoHibernate.java

License:Open Source License

@Override
public Puesto graba(Puesto puesto, Usuario usuario) {
    Session session = currentSession();
    if (usuario != null) {
        puesto.setEmpresa(usuario.getEmpresa());
    }//w w  w .ja v a  2 s. c o m
    session.saveOrUpdate(puesto);
    session.merge(puesto);
    session.flush();
    return puesto;
}

From source file:my.dao.HhDao.java

/***/
public <T> void merge(final T o) throws Exception {
    //return (T) sessionFactory.getCurrentSession().merge(o);
    //return (T) HibernateUtil.sessionFactory.openSession().merge(o);
    final Session session = HibernateUtil.sessionFactory.openSession();
    Transaction tx = null;//from w  w  w  .j a v a 2s  . co  m
    try {
        tx = session.beginTransaction();
        session.merge(o);
        tx.commit();
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        throw e;
    } finally {
        session.close();
    }
}

From source file:net.digitalprimates.persistence.hibernate.utils.services.HibernateService.java

License:Open Source License

public Object merge(Object object) {
    Session session = null;
    Object result = null;//from w ww.ja  v  a  2s.  c  o m

    try {
        session = HibernateUtil.getCurrentSession();

        long tStart = new Date().getTime();
        result = session.merge(object);
        long tEnd = new Date().getTime();
        log.debug("{save()}" + (tEnd - tStart) + "ms  class=" + object.getClass().getName());

    } catch (HibernateException ex) {
        HibernateUtil.rollbackTransaction();
        ex.printStackTrace();
        throw ex;
    } catch (RuntimeException ex) {
        HibernateUtil.rollbackTransaction();
        ex.printStackTrace();
        throw ex;
    }

    return result;
}

From source file:net.digitalprimates.persistence.hibernate.utils.services.HibernateService.java

License:Open Source License

public void delete(Object obj, boolean hardDelete) {
    Session session = null;

    try {/*from   w w w  .  ja v  a 2s.c om*/
        session = HibernateUtil.getCurrentSession();

        long tStart = new Date().getTime();

        if (hardDelete) {
            session.delete(obj);
        } else {
            // obj.setIsDeleted(0);
            session.merge(obj);
        }

        long tEnd = new Date().getTime();
        log.debug("{delete()}" + (tEnd - tStart) + "ms  class=" + obj.getClass().getName());

    } catch (HibernateException ex) {
        HibernateUtil.rollbackTransaction();
        ex.printStackTrace();
        throw ex;
    } catch (RuntimeException ex) {
        HibernateUtil.rollbackTransaction();
        ex.printStackTrace();
        throw ex;
    }
}

From source file:net.hybridcore.crowley.habbo.game.SessionManager.java

License:BEER-WARE LICENSE

public void markOffline(GameSession gameSession) {
    Habbo habbo = gameSession.getHabbo();

    if (this.habbos.containsKey(habbo.getId())) {
        Session session = DatastoreUtil.currentSession();
        updateMessenger(gameSession.getHabbo());

        habbo.setLastOnline(DateTime.now());
        habbo = (Habbo) session.merge(habbo);
        session.saveOrUpdate(habbo);//  w  ww .  j a v  a  2s.c  om
    }
}

From source file:net.hybridcore.crowley.habbo.messages.incoming.friendlist.AcceptBuddyMessageEvent.java

License:BEER-WARE LICENSE

public void run() {
    Session session = DatastoreUtil.currentSession();
    int count = this.clientMessage.readInt();

    for (int i = 0; i < count; i++) {
        int habboId = this.clientMessage.readInt();
        Habbo buddy = (Habbo) session.createCriteria(Habbo.class).add(Restrictions.idEq((long) habboId))
                .uniqueResult();//  w  ww  .ja  va  2 s.  c  o m

        // Invalid id, the fuck? We should probably delete this request at some point
        if (buddy == null) {
            break;
        }

        boolean valid = false;

        for (Habbo habbo : this.gameSession.getHabbo().getFriendRequests()) {
            if (habbo.getId().equals((long) habboId)) {
                valid = true;
            }
        }

        // Somebody is trying to add friends who didn't request it!
        if (!valid) {
            break;
        }

        if (buddy.isOnline()) {
            // fetch active session
            GameSession target = Crowley.getHabbo().getSessions().getSessionByHabboId(buddy.getId());
            buddy = target.getHabbo();

            buddy = (Habbo) session.merge(buddy);
            buddy.friendRequiresUpdate(this.gameSession.getHabbo().getId());
            session.saveOrUpdate(buddy);

            Crowley.getExecutorService().execute(new FriendListUpdateComposer(target));
        }

        this.gameSession.setHabbo((Habbo) session.merge(this.gameSession.getHabbo()));
        this.gameSession.getHabbo().addFriend(buddy);
        this.gameSession.getHabbo().friendRequiresUpdate(buddy.getId());

        session.saveOrUpdate(this.gameSession.getHabbo());

        Crowley.getExecutorService().execute(new FriendListUpdateComposer(this.gameSession));
    }
}

From source file:nl.strohalm.cyclos.dao.BaseDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T extends E> T update(final T entity, final boolean flush) {
    if (entity == null || entity.isTransient()) {
        throw new UnexpectedEntityException();
    }/*from   w w w.  j  a v  a 2s.  com*/
    try {
        hibernateQueryHandler.resolveReferences(entity);
        final T ret = getHibernateTemplate().execute(new HibernateCallback<T>() {
            public T doInHibernate(final Session session) throws HibernateException, SQLException {
                // As hibernate can have only 1 instance with a given id, if another instance with that id
                // is passed to update, it will throw a NonUniqueObjectException. So, we must merge the data
                try {
                    session.update(entity);
                    return entity;
                } catch (final NonUniqueObjectException e) {
                    // If there is another instance associated with the same id,
                    // merge the data on the associated instance...
                    session.merge(entity);
                    final Entity current = (Entity) session.load(EntityHelper.getRealClass(entity),
                            entity.getId());
                    // hibernateQueryHandler.copyProperties(entity, current);
                    // ... and return it
                    return (T) current;
                }
            }
        });
        if (flush) {
            flush();
        }

        // Ensure the second level cache is not getting stale
        evictSecondLevelCache();

        return ret;
    } catch (final ApplicationException e) {
        throw e;
    } catch (final Exception e) {
        throw new DaoException(e);
    }
}