public void edit(CheckoutRecord checkoutRecord) throws NonexistentEntityException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); CheckoutRecord persistentCheckoutRecord = em.find(CheckoutRecord.class, checkoutRecord.getId()); Member memberOld = persistentCheckoutRecord.getMember(); Member memberNew = checkoutRecord.getMember(); Fine fineOld = persistentCheckoutRecord.getFine(); Fine fineNew = checkoutRecord.getFine(); if (memberNew != null) { memberNew = em.getReference(memberNew.getClass(), memberNew.getUniqueMemberNumber()); checkoutRecord.setMember(memberNew); } if (fineNew != null) { fineNew = em.getReference(fineNew.getClass(), fineNew.getId()); checkoutRecord.setFine(fineNew); } checkoutRecord = em.merge(checkoutRecord); if (memberOld != null && !memberOld.equals(memberNew)) { memberOld.getRecords().remove(checkoutRecord); memberOld = em.merge(memberOld); } if (memberNew != null && !memberNew.equals(memberOld)) { memberNew.getRecords().add(checkoutRecord); memberNew = em.merge(memberNew); } if (fineOld != null && !fineOld.equals(fineNew)) { fineOld.setRecord(null); fineOld = em.merge(fineOld); } if (fineNew != null && !fineNew.equals(fineOld)) { CheckoutRecord oldRecordOfFine = fineNew.getRecord(); if (oldRecordOfFine != null) { oldRecordOfFine.setFine(null); oldRecordOfFine = em.merge(oldRecordOfFine); } fineNew.setRecord(checkoutRecord); fineNew = em.merge(fineNew); } em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { long id = checkoutRecord.getId(); if (findCheckoutRecord(id) == null) { throw new NonexistentEntityException( "The checkoutRecord with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } }
public void destroy(long id) throws NonexistentEntityException { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); CheckoutRecord checkoutRecord; try { checkoutRecord = em.getReference(CheckoutRecord.class, id); checkoutRecord.getId(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException( "The checkoutRecord with id " + id + " no longer exists.", enfe); } Member member = checkoutRecord.getMember(); if (member != null) { member.getRecords().remove(checkoutRecord); member = em.merge(member); } Fine fine = checkoutRecord.getFine(); if (fine != null) { fine.setRecord(null); fine = em.merge(fine); } em.remove(checkoutRecord); em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }