public void edit(Telefonos telefonos) throws NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Telefonos persistentTelefonos = em.find(Telefonos.class, telefonos.getId()); DatosPersonales codigoPersonaOld = persistentTelefonos.getCodigoPersona(); DatosPersonales codigoPersonaNew = telefonos.getCodigoPersona(); if (codigoPersonaNew != null) { codigoPersonaNew = em.getReference(codigoPersonaNew.getClass(), codigoPersonaNew.getCodigo()); telefonos.setCodigoPersona(codigoPersonaNew); } telefonos = em.merge(telefonos); if (codigoPersonaOld != null && !codigoPersonaOld.equals(codigoPersonaNew)) { codigoPersonaOld.getTelefonosCollection().remove(telefonos); codigoPersonaOld = em.merge(codigoPersonaOld); } if (codigoPersonaNew != null && !codigoPersonaNew.equals(codigoPersonaOld)) { codigoPersonaNew.getTelefonosCollection().add(telefonos); codigoPersonaNew = em.merge(codigoPersonaNew); } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { Integer id = telefonos.getId(); if (findTelefonos(id) == null) { throw new NonexistentEntityException( "The telefonos with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }
public void edit(Clientes clientes) throws IllegalOrphanException, NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Clientes persistentClientes = em.find(Clientes.class, clientes.getCodigo()); DatosPersonales codigoPersonaOld = persistentClientes.getCodigoPersona(); DatosPersonales codigoPersonaNew = clientes.getCodigoPersona(); Empresas codigoEmpOld = persistentClientes.getCodigoEmp(); Empresas codigoEmpNew = clientes.getCodigoEmp(); Collection<CliAlbCab> cliAlbCabCollectionOld = persistentClientes.getCliAlbCabCollection(); Collection<CliAlbCab> cliAlbCabCollectionNew = clientes.getCliAlbCabCollection(); Collection<CliFacCab> cliFacCabCollectionOld = persistentClientes.getCliFacCabCollection(); Collection<CliFacCab> cliFacCabCollectionNew = clientes.getCliFacCabCollection(); Collection<PreciosVentas> preciosVentasCollectionOld = persistentClientes.getPreciosVentasCollection(); Collection<PreciosVentas> preciosVentasCollectionNew = clientes.getPreciosVentasCollection(); Collection<CliPedidosCab> cliPedidosCabCollectionOld = persistentClientes.getCliPedidosCabCollection(); Collection<CliPedidosCab> cliPedidosCabCollectionNew = clientes.getCliPedidosCabCollection(); List<String> illegalOrphanMessages = null; for (CliAlbCab cliAlbCabCollectionOldCliAlbCab : cliAlbCabCollectionOld) { if (!cliAlbCabCollectionNew.contains(cliAlbCabCollectionOldCliAlbCab)) { if (illegalOrphanMessages == null) { illegalOrphanMessages = new ArrayList<String>(); } illegalOrphanMessages.add( "You must retain CliAlbCab " + cliAlbCabCollectionOldCliAlbCab + " since its codCli field is not nullable."); } } for (CliFacCab cliFacCabCollectionOldCliFacCab : cliFacCabCollectionOld) { if (!cliFacCabCollectionNew.contains(cliFacCabCollectionOldCliFacCab)) { if (illegalOrphanMessages == null) { illegalOrphanMessages = new ArrayList<String>(); } illegalOrphanMessages.add( "You must retain CliFacCab " + cliFacCabCollectionOldCliFacCab + " since its codCli field is not nullable."); } } for (PreciosVentas preciosVentasCollectionOldPreciosVentas : preciosVentasCollectionOld) { if (!preciosVentasCollectionNew.contains(preciosVentasCollectionOldPreciosVentas)) { if (illegalOrphanMessages == null) { illegalOrphanMessages = new ArrayList<String>(); } illegalOrphanMessages.add( "You must retain PreciosVentas " + preciosVentasCollectionOldPreciosVentas + " since its codCli field is not nullable."); } } for (CliPedidosCab cliPedidosCabCollectionOldCliPedidosCab : cliPedidosCabCollectionOld) { if (!cliPedidosCabCollectionNew.contains(cliPedidosCabCollectionOldCliPedidosCab)) { if (illegalOrphanMessages == null) { illegalOrphanMessages = new ArrayList<String>(); } illegalOrphanMessages.add( "You must retain CliPedidosCab " + cliPedidosCabCollectionOldCliPedidosCab + " since its codCli field is not nullable."); } } if (illegalOrphanMessages != null) { throw new IllegalOrphanException(illegalOrphanMessages); } if (codigoPersonaNew != null) { codigoPersonaNew = em.getReference(codigoPersonaNew.getClass(), codigoPersonaNew.getCodigo()); clientes.setCodigoPersona(codigoPersonaNew); } if (codigoEmpNew != null) { codigoEmpNew = em.getReference(codigoEmpNew.getClass(), codigoEmpNew.getCodigo()); clientes.setCodigoEmp(codigoEmpNew); } Collection<CliAlbCab> attachedCliAlbCabCollectionNew = new ArrayList<CliAlbCab>(); for (CliAlbCab cliAlbCabCollectionNewCliAlbCabToAttach : cliAlbCabCollectionNew) { cliAlbCabCollectionNewCliAlbCabToAttach = em.getReference( cliAlbCabCollectionNewCliAlbCabToAttach.getClass(), cliAlbCabCollectionNewCliAlbCabToAttach.getId()); attachedCliAlbCabCollectionNew.add(cliAlbCabCollectionNewCliAlbCabToAttach); } cliAlbCabCollectionNew = attachedCliAlbCabCollectionNew; clientes.setCliAlbCabCollection(cliAlbCabCollectionNew); Collection<CliFacCab> attachedCliFacCabCollectionNew = new ArrayList<CliFacCab>(); for (CliFacCab cliFacCabCollectionNewCliFacCabToAttach : cliFacCabCollectionNew) { cliFacCabCollectionNewCliFacCabToAttach = em.getReference( cliFacCabCollectionNewCliFacCabToAttach.getClass(), cliFacCabCollectionNewCliFacCabToAttach.getId()); attachedCliFacCabCollectionNew.add(cliFacCabCollectionNewCliFacCabToAttach); } cliFacCabCollectionNew = attachedCliFacCabCollectionNew; clientes.setCliFacCabCollection(cliFacCabCollectionNew); Collection<PreciosVentas> attachedPreciosVentasCollectionNew = new ArrayList<PreciosVentas>(); for (PreciosVentas preciosVentasCollectionNewPreciosVentasToAttach : preciosVentasCollectionNew) { preciosVentasCollectionNewPreciosVentasToAttach = em.getReference( preciosVentasCollectionNewPreciosVentasToAttach.getClass(), preciosVentasCollectionNewPreciosVentasToAttach.getId()); attachedPreciosVentasCollectionNew.add(preciosVentasCollectionNewPreciosVentasToAttach); } preciosVentasCollectionNew = attachedPreciosVentasCollectionNew; clientes.setPreciosVentasCollection(preciosVentasCollectionNew); Collection<CliPedidosCab> attachedCliPedidosCabCollectionNew = new ArrayList<CliPedidosCab>(); for (CliPedidosCab cliPedidosCabCollectionNewCliPedidosCabToAttach : cliPedidosCabCollectionNew) { cliPedidosCabCollectionNewCliPedidosCabToAttach = em.getReference( cliPedidosCabCollectionNewCliPedidosCabToAttach.getClass(), cliPedidosCabCollectionNewCliPedidosCabToAttach.getId()); attachedCliPedidosCabCollectionNew.add(cliPedidosCabCollectionNewCliPedidosCabToAttach); } cliPedidosCabCollectionNew = attachedCliPedidosCabCollectionNew; clientes.setCliPedidosCabCollection(cliPedidosCabCollectionNew); clientes = em.merge(clientes); if (codigoPersonaOld != null && !codigoPersonaOld.equals(codigoPersonaNew)) { codigoPersonaOld.getClientesCollection().remove(clientes); codigoPersonaOld = em.merge(codigoPersonaOld); } if (codigoPersonaNew != null && !codigoPersonaNew.equals(codigoPersonaOld)) { codigoPersonaNew.getClientesCollection().add(clientes); codigoPersonaNew = em.merge(codigoPersonaNew); } if (codigoEmpOld != null && !codigoEmpOld.equals(codigoEmpNew)) { codigoEmpOld.getClientesCollection().remove(clientes); codigoEmpOld = em.merge(codigoEmpOld); } if (codigoEmpNew != null && !codigoEmpNew.equals(codigoEmpOld)) { codigoEmpNew.getClientesCollection().add(clientes); codigoEmpNew = em.merge(codigoEmpNew); } for (CliAlbCab cliAlbCabCollectionNewCliAlbCab : cliAlbCabCollectionNew) { if (!cliAlbCabCollectionOld.contains(cliAlbCabCollectionNewCliAlbCab)) { Clientes oldCodCliOfCliAlbCabCollectionNewCliAlbCab = cliAlbCabCollectionNewCliAlbCab.getCodCli(); cliAlbCabCollectionNewCliAlbCab.setCodCli(clientes); cliAlbCabCollectionNewCliAlbCab = em.merge(cliAlbCabCollectionNewCliAlbCab); if (oldCodCliOfCliAlbCabCollectionNewCliAlbCab != null && !oldCodCliOfCliAlbCabCollectionNewCliAlbCab.equals(clientes)) { oldCodCliOfCliAlbCabCollectionNewCliAlbCab .getCliAlbCabCollection() .remove(cliAlbCabCollectionNewCliAlbCab); oldCodCliOfCliAlbCabCollectionNewCliAlbCab = em.merge(oldCodCliOfCliAlbCabCollectionNewCliAlbCab); } } } for (CliFacCab cliFacCabCollectionNewCliFacCab : cliFacCabCollectionNew) { if (!cliFacCabCollectionOld.contains(cliFacCabCollectionNewCliFacCab)) { Clientes oldCodCliOfCliFacCabCollectionNewCliFacCab = cliFacCabCollectionNewCliFacCab.getCodCli(); cliFacCabCollectionNewCliFacCab.setCodCli(clientes); cliFacCabCollectionNewCliFacCab = em.merge(cliFacCabCollectionNewCliFacCab); if (oldCodCliOfCliFacCabCollectionNewCliFacCab != null && !oldCodCliOfCliFacCabCollectionNewCliFacCab.equals(clientes)) { oldCodCliOfCliFacCabCollectionNewCliFacCab .getCliFacCabCollection() .remove(cliFacCabCollectionNewCliFacCab); oldCodCliOfCliFacCabCollectionNewCliFacCab = em.merge(oldCodCliOfCliFacCabCollectionNewCliFacCab); } } } for (PreciosVentas preciosVentasCollectionNewPreciosVentas : preciosVentasCollectionNew) { if (!preciosVentasCollectionOld.contains(preciosVentasCollectionNewPreciosVentas)) { Clientes oldCodCliOfPreciosVentasCollectionNewPreciosVentas = preciosVentasCollectionNewPreciosVentas.getCodCli(); preciosVentasCollectionNewPreciosVentas.setCodCli(clientes); preciosVentasCollectionNewPreciosVentas = em.merge(preciosVentasCollectionNewPreciosVentas); if (oldCodCliOfPreciosVentasCollectionNewPreciosVentas != null && !oldCodCliOfPreciosVentasCollectionNewPreciosVentas.equals(clientes)) { oldCodCliOfPreciosVentasCollectionNewPreciosVentas .getPreciosVentasCollection() .remove(preciosVentasCollectionNewPreciosVentas); oldCodCliOfPreciosVentasCollectionNewPreciosVentas = em.merge(oldCodCliOfPreciosVentasCollectionNewPreciosVentas); } } } for (CliPedidosCab cliPedidosCabCollectionNewCliPedidosCab : cliPedidosCabCollectionNew) { if (!cliPedidosCabCollectionOld.contains(cliPedidosCabCollectionNewCliPedidosCab)) { Clientes oldCodCliOfCliPedidosCabCollectionNewCliPedidosCab = cliPedidosCabCollectionNewCliPedidosCab.getCodCli(); cliPedidosCabCollectionNewCliPedidosCab.setCodCli(clientes); cliPedidosCabCollectionNewCliPedidosCab = em.merge(cliPedidosCabCollectionNewCliPedidosCab); if (oldCodCliOfCliPedidosCabCollectionNewCliPedidosCab != null && !oldCodCliOfCliPedidosCabCollectionNewCliPedidosCab.equals(clientes)) { oldCodCliOfCliPedidosCabCollectionNewCliPedidosCab .getCliPedidosCabCollection() .remove(cliPedidosCabCollectionNewCliPedidosCab); oldCodCliOfCliPedidosCabCollectionNewCliPedidosCab = em.merge(oldCodCliOfCliPedidosCabCollectionNewCliPedidosCab); } } } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { String id = clientes.getCodigo(); if (findClientes(id) == null) { throw new NonexistentEntityException("The clientes with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }