1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public void edit(Candidat candidat) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Candidat persistentCandidat = em.find(Candidat.class, candidat.getIdCandidat());
Collection<Poste> posteCollectionOld = persistentCandidat.getPosteCollection();
Collection<Poste> posteCollectionNew = candidat.getPosteCollection();
Collection<Poste> attachedPosteCollectionNew = new ArrayList<Poste>();
for (Poste posteCollectionNewPosteToAttach : posteCollectionNew) {
posteCollectionNewPosteToAttach = em.getReference(posteCollectionNewPosteToAttach.getClass(), posteCollectionNewPosteToAttach.getIdPoste());
attachedPosteCollectionNew.add(posteCollectionNewPosteToAttach);
}
posteCollectionNew = attachedPosteCollectionNew;
candidat.setPosteCollection(posteCollectionNew);
candidat = em.merge(candidat);
/*ligne 79*/ for (Poste posteCollectionOldPoste : posteCollectionOld) {
if (!posteCollectionNew.contains(posteCollectionOldPoste)) {
posteCollectionOldPoste.getCandidatCollection().remove(candidat);
posteCollectionOldPoste = em.merge(posteCollectionOldPoste);
}
}
for (Poste posteCollectionNewPoste : posteCollectionNew) {
if (!posteCollectionOld.contains(posteCollectionNewPoste)) {
posteCollectionNewPoste.getCandidatCollection().add(candidat);
posteCollectionNewPoste = em.merge(posteCollectionNewPoste);
}
}
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = candidat.getIdCandidat();
if (findCandidat(id) == null) {
throw new NonexistentEntityException("The candidat with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
} |
Partager