Ejemplo n.º 1
0
 public void create(BarEntity bar) throws PreexistingEntityException, Exception {
   EntityManager em = null;
   try {
     em = getEntityManager();
     em.getTransaction().begin();
     ContractEntity contract = bar.getContract();
     if (contract != null) {
       contract = em.getReference(contract.getClass(), contract.getId());
       bar.setContract(contract);
     }
     em.persist(bar);
     if (contract != null) {
       contract.getBarCollection().add(bar);
       contract = em.merge(contract);
     }
     em.getTransaction().commit();
   } catch (Exception ex) {
     if (findBar(bar.getId()) != null) {
       throw new PreexistingEntityException("Bar " + bar + " already exists.", ex);
     }
     throw ex;
   } finally {
     if (em != null /*&& em.isOpen()*/) {
       em.close();
     }
   }
 }
Ejemplo n.º 2
0
 public void edit(BarEntity bar) throws NonexistentEntityException, Exception {
   EntityManager em = null;
   try {
     em = getEntityManager();
     em.getTransaction().begin();
     BarEntity persistentBar = em.find(BarEntity.class, bar.getId());
     ContractEntity contractOld = persistentBar.getContract();
     ContractEntity contractNew = bar.getContract();
     if (contractNew != null) {
       contractNew = em.getReference(contractNew.getClass(), contractNew.getId());
       bar.setContract(contractNew);
     }
     bar = em.merge(bar);
     if (contractOld != null && !contractOld.equals(contractNew)) {
       contractOld.getBarCollection().remove(bar);
       contractOld = em.merge(contractOld);
     }
     if (contractNew != null && !contractNew.equals(contractOld)) {
       contractNew.getBarCollection().add(bar);
       contractNew = em.merge(contractNew);
     }
     em.getTransaction().commit();
   } catch (Exception ex) {
     String msg = ex.getLocalizedMessage();
     if (msg == null || msg.length() == 0) {
       Integer id = bar.getId();
       if (findBar(id) == null) {
         throw new NonexistentEntityException("The bar with id " + id + " no longer exists.");
       }
     }
     throw ex;
   } finally {
     if (em != null) {
       em.close();
     }
   }
 }