public void edit(Usuario usuario) throws NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Usuario persistentUsuario = em.find(Usuario.class, usuario.getNombre()); Investigador investigadorIdentificacionOld = persistentUsuario.getInvestigadorIdentificacion(); Investigador investigadorIdentificacionNew = usuario.getInvestigadorIdentificacion(); if (investigadorIdentificacionNew != null) { investigadorIdentificacionNew = em.getReference( investigadorIdentificacionNew.getClass(), investigadorIdentificacionNew.getIdentificacion()); usuario.setInvestigadorIdentificacion(investigadorIdentificacionNew); } usuario = em.merge(usuario); if (investigadorIdentificacionOld != null && !investigadorIdentificacionOld.equals(investigadorIdentificacionNew)) { investigadorIdentificacionOld.getUsuarioList().remove(usuario); investigadorIdentificacionOld = em.merge(investigadorIdentificacionOld); } if (investigadorIdentificacionNew != null && !investigadorIdentificacionNew.equals(investigadorIdentificacionOld)) { investigadorIdentificacionNew.getUsuarioList().add(usuario); investigadorIdentificacionNew = em.merge(investigadorIdentificacionNew); } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { String id = usuario.getNombre(); if (findUsuario(id) == null) { throw new NonexistentEntityException("The usuario with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }