public void edit(CategHabitacion categHabitacion) throws NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); CategHabitacion persistentCategHabitacion = em.find(CategHabitacion.class, categHabitacion.getCodigoCategoria()); Collection<Habitacion> habitacionCollectionOld = persistentCategHabitacion.getHabitacionCollection(); Collection<Habitacion> habitacionCollectionNew = categHabitacion.getHabitacionCollection(); Collection<Habitacion> attachedHabitacionCollectionNew = new ArrayList<Habitacion>(); for (Habitacion habitacionCollectionNewHabitacionToAttach : habitacionCollectionNew) { habitacionCollectionNewHabitacionToAttach = em.getReference( habitacionCollectionNewHabitacionToAttach.getClass(), habitacionCollectionNewHabitacionToAttach.getNumero()); attachedHabitacionCollectionNew.add(habitacionCollectionNewHabitacionToAttach); } habitacionCollectionNew = attachedHabitacionCollectionNew; categHabitacion.setHabitacionCollection(habitacionCollectionNew); categHabitacion = em.merge(categHabitacion); for (Habitacion habitacionCollectionOldHabitacion : habitacionCollectionOld) { if (!habitacionCollectionNew.contains(habitacionCollectionOldHabitacion)) { habitacionCollectionOldHabitacion.setCodigoCategoria(null); habitacionCollectionOldHabitacion = em.merge(habitacionCollectionOldHabitacion); } } for (Habitacion habitacionCollectionNewHabitacion : habitacionCollectionNew) { if (!habitacionCollectionOld.contains(habitacionCollectionNewHabitacion)) { CategHabitacion oldCodigoCategoriaOfHabitacionCollectionNewHabitacion = habitacionCollectionNewHabitacion.getCodigoCategoria(); habitacionCollectionNewHabitacion.setCodigoCategoria(categHabitacion); habitacionCollectionNewHabitacion = em.merge(habitacionCollectionNewHabitacion); if (oldCodigoCategoriaOfHabitacionCollectionNewHabitacion != null && !oldCodigoCategoriaOfHabitacionCollectionNewHabitacion.equals(categHabitacion)) { oldCodigoCategoriaOfHabitacionCollectionNewHabitacion .getHabitacionCollection() .remove(habitacionCollectionNewHabitacion); oldCodigoCategoriaOfHabitacionCollectionNewHabitacion = em.merge(oldCodigoCategoriaOfHabitacionCollectionNewHabitacion); } } } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { Integer id = categHabitacion.getCodigoCategoria(); if (findCategHabitacion(id) == null) { throw new NonexistentEntityException( "The categHabitacion with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }
public void destroy(Integer id) throws NonexistentEntityException { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); CategHabitacion categHabitacion; try { categHabitacion = em.getReference(CategHabitacion.class, id); categHabitacion.getCodigoCategoria(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException( "The categHabitacion with id " + id + " no longer exists.", enfe); } Collection<Habitacion> habitacionCollection = categHabitacion.getHabitacionCollection(); for (Habitacion habitacionCollectionHabitacion : habitacionCollection) { habitacionCollectionHabitacion.setCodigoCategoria(null); habitacionCollectionHabitacion = em.merge(habitacionCollectionHabitacion); } em.remove(categHabitacion); em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }
public void create(CategHabitacion categHabitacion) { if (categHabitacion.getHabitacionCollection() == null) { categHabitacion.setHabitacionCollection(new ArrayList<Habitacion>()); } EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Collection<Habitacion> attachedHabitacionCollection = new ArrayList<Habitacion>(); for (Habitacion habitacionCollectionHabitacionToAttach : categHabitacion.getHabitacionCollection()) { habitacionCollectionHabitacionToAttach = em.getReference( habitacionCollectionHabitacionToAttach.getClass(), habitacionCollectionHabitacionToAttach.getNumero()); attachedHabitacionCollection.add(habitacionCollectionHabitacionToAttach); } categHabitacion.setHabitacionCollection(attachedHabitacionCollection); em.persist(categHabitacion); for (Habitacion habitacionCollectionHabitacion : categHabitacion.getHabitacionCollection()) { CategHabitacion oldCodigoCategoriaOfHabitacionCollectionHabitacion = habitacionCollectionHabitacion.getCodigoCategoria(); habitacionCollectionHabitacion.setCodigoCategoria(categHabitacion); habitacionCollectionHabitacion = em.merge(habitacionCollectionHabitacion); if (oldCodigoCategoriaOfHabitacionCollectionHabitacion != null) { oldCodigoCategoriaOfHabitacionCollectionHabitacion .getHabitacionCollection() .remove(habitacionCollectionHabitacion); oldCodigoCategoriaOfHabitacionCollectionHabitacion = em.merge(oldCodigoCategoriaOfHabitacionCollectionHabitacion); } } em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }