public void destroy(Integer id) throws NonexistentEntityException { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Carrera carrera; try { carrera = em.getReference(Carrera.class, id); carrera.getIdCarrera(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException( "The carrera with id " + id + " no longer exists.", enfe); } Collection<Alumno> alumnoCollection = carrera.getAlumnoCollection(); for (Alumno alumnoCollectionAlumno : alumnoCollection) { alumnoCollectionAlumno.setIdCarreraFK(null); alumnoCollectionAlumno = em.merge(alumnoCollectionAlumno); } em.remove(carrera); em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }
public void edit(Carrera carrera) throws NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Carrera persistentCarrera = em.find(Carrera.class, carrera.getIdCarrera()); Collection<Alumno> alumnoCollectionOld = persistentCarrera.getAlumnoCollection(); Collection<Alumno> alumnoCollectionNew = carrera.getAlumnoCollection(); Collection<Alumno> attachedAlumnoCollectionNew = new ArrayList<Alumno>(); for (Alumno alumnoCollectionNewAlumnoToAttach : alumnoCollectionNew) { alumnoCollectionNewAlumnoToAttach = em.getReference( alumnoCollectionNewAlumnoToAttach.getClass(), alumnoCollectionNewAlumnoToAttach.getIdAlumnos()); attachedAlumnoCollectionNew.add(alumnoCollectionNewAlumnoToAttach); } alumnoCollectionNew = attachedAlumnoCollectionNew; carrera.setAlumnoCollection(alumnoCollectionNew); carrera = em.merge(carrera); for (Alumno alumnoCollectionOldAlumno : alumnoCollectionOld) { if (!alumnoCollectionNew.contains(alumnoCollectionOldAlumno)) { alumnoCollectionOldAlumno.setIdCarreraFK(null); alumnoCollectionOldAlumno = em.merge(alumnoCollectionOldAlumno); } } for (Alumno alumnoCollectionNewAlumno : alumnoCollectionNew) { if (!alumnoCollectionOld.contains(alumnoCollectionNewAlumno)) { Carrera oldIdCarreraFKOfAlumnoCollectionNewAlumno = alumnoCollectionNewAlumno.getIdCarreraFK(); alumnoCollectionNewAlumno.setIdCarreraFK(carrera); alumnoCollectionNewAlumno = em.merge(alumnoCollectionNewAlumno); if (oldIdCarreraFKOfAlumnoCollectionNewAlumno != null && !oldIdCarreraFKOfAlumnoCollectionNewAlumno.equals(carrera)) { oldIdCarreraFKOfAlumnoCollectionNewAlumno .getAlumnoCollection() .remove(alumnoCollectionNewAlumno); oldIdCarreraFKOfAlumnoCollectionNewAlumno = em.merge(oldIdCarreraFKOfAlumnoCollectionNewAlumno); } } } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { Integer id = carrera.getIdCarrera(); if (findCarrera(id) == null) { throw new NonexistentEntityException("The carrera with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }
public void create(Carrera carrera) throws PreexistingEntityException, Exception { if (carrera.getAlumnoCollection() == null) { carrera.setAlumnoCollection(new ArrayList<Alumno>()); } EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Collection<Alumno> attachedAlumnoCollection = new ArrayList<Alumno>(); for (Alumno alumnoCollectionAlumnoToAttach : carrera.getAlumnoCollection()) { alumnoCollectionAlumnoToAttach = em.getReference( alumnoCollectionAlumnoToAttach.getClass(), alumnoCollectionAlumnoToAttach.getIdAlumnos()); attachedAlumnoCollection.add(alumnoCollectionAlumnoToAttach); } carrera.setAlumnoCollection(attachedAlumnoCollection); em.persist(carrera); for (Alumno alumnoCollectionAlumno : carrera.getAlumnoCollection()) { Carrera oldIdCarreraFKOfAlumnoCollectionAlumno = alumnoCollectionAlumno.getIdCarreraFK(); alumnoCollectionAlumno.setIdCarreraFK(carrera); alumnoCollectionAlumno = em.merge(alumnoCollectionAlumno); if (oldIdCarreraFKOfAlumnoCollectionAlumno != null) { oldIdCarreraFKOfAlumnoCollectionAlumno .getAlumnoCollection() .remove(alumnoCollectionAlumno); oldIdCarreraFKOfAlumnoCollectionAlumno = em.merge(oldIdCarreraFKOfAlumnoCollectionAlumno); } } em.getTransaction().commit(); } catch (Exception ex) { if (findCarrera(carrera.getIdCarrera()) != null) { throw new PreexistingEntityException("Carrera " + carrera + " already exists.", ex); } throw ex; } finally { if (em != null) { em.close(); } } }