public void create(Livreurs livreurs) { if (livreurs.getClientsList() == null) { livreurs.setClientsList(new ArrayList<Clients>()); } EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Statuts statut = livreurs.getStatut(); if (statut != null) { statut = em.getReference(statut.getClass(), statut.getIdStatut()); livreurs.setStatut(statut); } List<Clients> attachedClientsList = new ArrayList<Clients>(); for (Clients clientsListClientsToAttach : livreurs.getClientsList()) { clientsListClientsToAttach = em.getReference( clientsListClientsToAttach.getClass(), clientsListClientsToAttach.getIdClient()); attachedClientsList.add(clientsListClientsToAttach); } livreurs.setClientsList(attachedClientsList); em.persist(livreurs); if (statut != null) { statut.getLivreursList().add(livreurs); statut = em.merge(statut); } for (Clients clientsListClients : livreurs.getClientsList()) { Livreurs oldIdLivreurOfClientsListClients = clientsListClients.getIdLivreur(); clientsListClients.setIdLivreur(livreurs); clientsListClients = em.merge(clientsListClients); if (oldIdLivreurOfClientsListClients != null) { oldIdLivreurOfClientsListClients.getClientsList().remove(clientsListClients); oldIdLivreurOfClientsListClients = em.merge(oldIdLivreurOfClientsListClients); } } em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }
public void edit(Livreurs livreurs) throws NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Livreurs persistentLivreurs = em.find(Livreurs.class, livreurs.getIdLivreur()); Statuts statutOld = persistentLivreurs.getStatut(); Statuts statutNew = livreurs.getStatut(); List<Clients> clientsListOld = persistentLivreurs.getClientsList(); List<Clients> clientsListNew = livreurs.getClientsList(); if (statutNew != null) { statutNew = em.getReference(statutNew.getClass(), statutNew.getIdStatut()); livreurs.setStatut(statutNew); } List<Clients> attachedClientsListNew = new ArrayList<Clients>(); for (Clients clientsListNewClientsToAttach : clientsListNew) { clientsListNewClientsToAttach = em.getReference( clientsListNewClientsToAttach.getClass(), clientsListNewClientsToAttach.getIdClient()); attachedClientsListNew.add(clientsListNewClientsToAttach); } clientsListNew = attachedClientsListNew; livreurs.setClientsList(clientsListNew); livreurs = em.merge(livreurs); if (statutOld != null && !statutOld.equals(statutNew)) { statutOld.getLivreursList().remove(livreurs); statutOld = em.merge(statutOld); } if (statutNew != null && !statutNew.equals(statutOld)) { statutNew.getLivreursList().add(livreurs); statutNew = em.merge(statutNew); } for (Clients clientsListOldClients : clientsListOld) { if (!clientsListNew.contains(clientsListOldClients)) { clientsListOldClients.setIdLivreur(null); clientsListOldClients = em.merge(clientsListOldClients); } } for (Clients clientsListNewClients : clientsListNew) { if (!clientsListOld.contains(clientsListNewClients)) { Livreurs oldIdLivreurOfClientsListNewClients = clientsListNewClients.getIdLivreur(); clientsListNewClients.setIdLivreur(livreurs); clientsListNewClients = em.merge(clientsListNewClients); if (oldIdLivreurOfClientsListNewClients != null && !oldIdLivreurOfClientsListNewClients.equals(livreurs)) { oldIdLivreurOfClientsListNewClients.getClientsList().remove(clientsListNewClients); oldIdLivreurOfClientsListNewClients = em.merge(oldIdLivreurOfClientsListNewClients); } } } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { Integer id = livreurs.getIdLivreur(); if (findLivreurs(id) == null) { throw new NonexistentEntityException("The livreurs with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }