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 create(CheckoutRecord checkoutRecord) { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); Member member = checkoutRecord.getMember(); if (member != null) { member = em.getReference(member.getClass(), member.getUniqueMemberNumber()); checkoutRecord.setMember(member); } Fine fine = checkoutRecord.getFine(); if (fine != null) { fine = em.getReference(fine.getClass(), fine.getId()); checkoutRecord.setFine(fine); } em.persist(checkoutRecord); if (member != null) { member.getRecords().add(checkoutRecord); member = em.merge(member); } if (fine != null) { CheckoutRecord oldRecordOfFine = fine.getRecord(); if (oldRecordOfFine != null) { oldRecordOfFine.setFine(null); oldRecordOfFine = em.merge(oldRecordOfFine); } fine.setRecord(checkoutRecord); fine = em.merge(fine); } em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }