public void edit(Inventary inventary) throws NonexistentEntityException, Exception {
   EntityManager em = null;
   try {
     em = getEntityManager();
     em.getTransaction().begin();
     Inventary persistentInventary = em.find(Inventary.class, inventary.getId());
     Product productIdOld = persistentInventary.getProductId();
     Product productIdNew = inventary.getProductId();
     Billing billingIdOld = persistentInventary.getBillingId();
     Billing billingIdNew = inventary.getBillingId();
     if (productIdNew != null) {
       productIdNew = em.getReference(productIdNew.getClass(), productIdNew.getId());
       inventary.setProductId(productIdNew);
     }
     if (billingIdNew != null) {
       billingIdNew = em.getReference(billingIdNew.getClass(), billingIdNew.getId());
       inventary.setBillingId(billingIdNew);
     }
     inventary = em.merge(inventary);
     if (productIdOld != null && !productIdOld.equals(productIdNew)) {
       productIdOld.getInventaryList().remove(inventary);
       productIdOld = em.merge(productIdOld);
     }
     if (productIdNew != null && !productIdNew.equals(productIdOld)) {
       productIdNew.getInventaryList().add(inventary);
       productIdNew = em.merge(productIdNew);
     }
     if (billingIdOld != null && !billingIdOld.equals(billingIdNew)) {
       billingIdOld.getInventaryList().remove(inventary);
       billingIdOld = em.merge(billingIdOld);
     }
     if (billingIdNew != null && !billingIdNew.equals(billingIdOld)) {
       billingIdNew.getInventaryList().add(inventary);
       billingIdNew = em.merge(billingIdNew);
     }
     em.getTransaction().commit();
   } catch (Exception ex) {
     String msg = ex.getLocalizedMessage();
     if (msg == null || msg.length() == 0) {
       Integer id = inventary.getId();
       if (findInventary(id) == null) {
         throw new NonexistentEntityException(
             "The inventary with id " + id + " no longer exists.");
       }
     }
     throw ex;
   } finally {
     if (em != null) {
       em.close();
     }
   }
 }
 public void create(Inventary inventary) throws PreexistingEntityException, Exception {
   EntityManager em = null;
   try {
     em = getEntityManager();
     em.getTransaction().begin();
     Product productId = inventary.getProductId();
     if (productId != null) {
       productId = em.getReference(productId.getClass(), productId.getId());
       inventary.setProductId(productId);
     }
     Billing billingId = inventary.getBillingId();
     if (billingId != null) {
       billingId = em.getReference(billingId.getClass(), billingId.getId());
       inventary.setBillingId(billingId);
     }
     em.persist(inventary);
     if (productId != null) {
       productId.getInventaryList().add(inventary);
       productId = em.merge(productId);
     }
     if (billingId != null) {
       billingId.getInventaryList().add(inventary);
       billingId = em.merge(billingId);
     }
     em.getTransaction().commit();
   } catch (Exception ex) {
     if (findInventary(inventary.getId()) != null) {
       throw new PreexistingEntityException("Inventary " + inventary + " already exists.", ex);
     }
     throw ex;
   } finally {
     if (em != null) {
       em.close();
     }
   }
 }