public void create(Proveedores proveedores) { if (proveedores.getFacturasCollection() == null) { proveedores.setFacturasCollection(new ArrayList<Facturas>()); } EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Collection<Facturas> attachedFacturasCollection = new ArrayList<Facturas>(); for (Facturas facturasCollectionFacturasToAttach : proveedores.getFacturasCollection()) { facturasCollectionFacturasToAttach = em.getReference( facturasCollectionFacturasToAttach.getClass(), facturasCollectionFacturasToAttach.getId()); attachedFacturasCollection.add(facturasCollectionFacturasToAttach); } proveedores.setFacturasCollection(attachedFacturasCollection); em.persist(proveedores); for (Facturas facturasCollectionFacturas : proveedores.getFacturasCollection()) { Proveedores oldIdProveedorOfFacturasCollectionFacturas = facturasCollectionFacturas.getIdProveedor(); facturasCollectionFacturas.setIdProveedor(proveedores); facturasCollectionFacturas = em.merge(facturasCollectionFacturas); if (oldIdProveedorOfFacturasCollectionFacturas != null) { oldIdProveedorOfFacturasCollectionFacturas .getFacturasCollection() .remove(facturasCollectionFacturas); oldIdProveedorOfFacturasCollectionFacturas = em.merge(oldIdProveedorOfFacturasCollectionFacturas); } } em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }
public void edit(Proveedores proveedores) throws IllegalOrphanException, NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Proveedores persistentProveedores = em.find(Proveedores.class, proveedores.getId()); Collection<Facturas> facturasCollectionOld = persistentProveedores.getFacturasCollection(); Collection<Facturas> facturasCollectionNew = proveedores.getFacturasCollection(); List<String> illegalOrphanMessages = null; for (Facturas facturasCollectionOldFacturas : facturasCollectionOld) { if (!facturasCollectionNew.contains(facturasCollectionOldFacturas)) { if (illegalOrphanMessages == null) { illegalOrphanMessages = new ArrayList<String>(); } illegalOrphanMessages.add( "You must retain Facturas " + facturasCollectionOldFacturas + " since its idProveedor field is not nullable."); } } if (illegalOrphanMessages != null) { throw new IllegalOrphanException(illegalOrphanMessages); } Collection<Facturas> attachedFacturasCollectionNew = new ArrayList<Facturas>(); for (Facturas facturasCollectionNewFacturasToAttach : facturasCollectionNew) { facturasCollectionNewFacturasToAttach = em.getReference( facturasCollectionNewFacturasToAttach.getClass(), facturasCollectionNewFacturasToAttach.getId()); attachedFacturasCollectionNew.add(facturasCollectionNewFacturasToAttach); } facturasCollectionNew = attachedFacturasCollectionNew; proveedores.setFacturasCollection(facturasCollectionNew); proveedores = em.merge(proveedores); for (Facturas facturasCollectionNewFacturas : facturasCollectionNew) { if (!facturasCollectionOld.contains(facturasCollectionNewFacturas)) { Proveedores oldIdProveedorOfFacturasCollectionNewFacturas = facturasCollectionNewFacturas.getIdProveedor(); facturasCollectionNewFacturas.setIdProveedor(proveedores); facturasCollectionNewFacturas = em.merge(facturasCollectionNewFacturas); if (oldIdProveedorOfFacturasCollectionNewFacturas != null && !oldIdProveedorOfFacturasCollectionNewFacturas.equals(proveedores)) { oldIdProveedorOfFacturasCollectionNewFacturas .getFacturasCollection() .remove(facturasCollectionNewFacturas); oldIdProveedorOfFacturasCollectionNewFacturas = em.merge(oldIdProveedorOfFacturasCollectionNewFacturas); } } } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { Integer id = proveedores.getId(); if (findProveedores(id) == null) { throw new NonexistentEntityException( "The proveedores with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }