title: JPA how to persist parent in bi-directional ManyToOne
body: <p>I have Payment entity with bi-directional ManyToOne releshionship to Account.</p>
  
  <pre><code>@Table(name="account")
  public class Account implements Serializable { 
  
      //bi-directional many-to-one association to Payment
      @OneToMany(mappedBy="account",fetch=FetchType.EAGER)
      private List&lt;Payment&gt; payments;
  
  @Table(name="payment")
  public class Payment implements Serializable { 
  
      //bi-directional many-to-one association to Account
      @ManyToOne(cascade={CascadeType.MERGE},fetch=FetchType.EAGER)
      @JoinColumn(name="idAcc")
      private Account account;
  </code></pre>
  
  <p>Account entity was persisted. And I see information about that in the browser and in database:</p>
  
  <blockquote>
    <p>Account [idAcc=475, account=12345678901230, isLock=N, rest=10000.5]</p>
  </blockquote>
  
  <p>Then I need to persist child (Payment entity) and simultaneously change Account entity (change account rest). I use this code.</p>
  
  <pre><code>    public class GenericDaoImpl&lt;T&gt; implements GenericDao&lt;T&gt; {
  
          protected Class&lt;T&gt; type; 
          protected EntityManagerFactory emf = null;
  
          public GenericDaoImpl(Class&lt;T&gt; type, EntityManagerFactory emf) { 
              this.emf = emf;
              this.type = type;       
          } 
  
          @Override
          public void create(T entity) throws Exception { 
              EntityManager em = null; 
              try { 
                  em = getEntityManager();
                  em.getTransaction().begin();  
                  em.persist(entity); 
                  em.getTransaction().commit(); 
              } 
  ...
  @Override
      public T findById(String id) {
           EntityManager em = getEntityManager();
              try {
                  Query query = em.createNamedQuery(type.getSimpleName()+".findByName");
                  query.setParameter("id", id);
                  return (T)query.getSingleResult();
              } finally {
                  em.close();
              }
      }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>daoPayments = new GenericDaoImpl(Payment.class,factory); 
              Payment payment = null;
                  try {
                      payment = new Payment();
                      payment.setDescription("Shop 'Pirasmani'");
                      payment.setSumm(50.25);
                          Account account = (Account)daoAccount.findById(listAccount.get(0).getIdAcc());
                          account.setRest(account.getRest()-payment.getSumm());
                          payment.setAccount(account);
                          account.getPayments().add(payment);
  
                      daoPayments.create(payment);
                      //print result 
                      Payment paymentMerged = (Payment)daoPayments.read(payment);
                      out.println(paymentMerged.toString()+"&lt;br&gt;"); 
  </code></pre>
  
  <p>Then I see in the browser that account rest was changed:</p>
  
  <blockquote>
    <p>Payment [idPmnt=91, description=Shop 'Pirasmani', summ=50.25,
    account=Account [idAcc=475, account=12345678901230, isLock=N,
    rest=9950.25]]</p>
  </blockquote>
  
  <p>But in database there is no changes in account rest. It steel is =10000.5.
  What am I doing wrong? Thanks.</p>