title: JPA: Parent changes not reflected when querying a child
body: <p>I have two entites <code>X</code>and <code>Y</code>defined as follows. <code>Y</code> has a many to one relationship with <code>X</code>:</p>
  
  <pre><code>public class X {
      @Id
      @Column(name = "xId", unique = true, nullable = false, length = 50)
      private Integer id;
      @Column(name = "description", nullable = true, length = 50)
      private String description;
      ...
  }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>public class Y {
      @Id
      @Column(name = "yId", unique = true, nullable = false, length = 50)
      private Integer id;
      @ManyToOne(fetch=FetchType.EAGER)
      @JoinColumn(name="x", nullable=false)
      private X x;
      ...
  }
  </code></pre>
  
  <p>When I update an entity <code>X</code>, the changes are not reflected correctly when I load <code>Y</code>. Assume theres already a row on the X table with the values (1, "2222"). Printing Y the first time will show X as (1, "2222"). However after the commit I see the X table at the DB has changed, but Y i still linked to the old version of X when queried again: </p>
  
  <pre><code>Y y = yDao.findById(ABC);
  System.out.println(y);  // y.x.description is "2222"
  
  xDao.beginTransaction();
  X x = new X(1, "4444");
  xDao.update(x);
  xDao.commitTransaction();   
  
  Y y2 = yDao.findById(ABC);  // y.x.description is still "2222" instead of "4444"
  System.out.println(y2);
  </code></pre>
  
  <p>My conclusion is Y is being retrieved from the cache the second time. What am I missing in order to make Y be aware that X has changed?</p>
  
  <p>In short, y is equal to y2, meaning my second find is retrieving its info from cache?</p>
  
  <p>What am I missing in order to make Y be aware that X has changed?</p>
  
  <p>Adding output sql:</p>
  
  <pre><code>Hibernate: // first findById()
      select
          y0_.yId as yId12_1_,
          y0_.address as address12_1_,
          y0_.x as x12_1_,
          x1_.xId as xId17_0_,
          x1_.description as descript2_17_0_ 
      from
          daoTest.Y y0_ 
      inner join
          daoTest.X x1_ 
              on y0_.x=x1_.xId 
      where
          y0_.yId=?
  Y [id=11, x=X [id=1, description=0000]]   // System.out
  Hibernate: // merge(x)
      select
          x0_.xId as xId5_0_,
          x0_.description as descript2_5_0_ 
      from
          daoTest.X x0_ 
      where
          x0_.xId=?
  Hibernate: // commitTransaction()
      update
          daoTest.X 
      set
          description=? 
      where
          xId=?
  Y [id=11, x=X [id=1, description=0000]]   //System.out, theres no select again
  </code></pre>
  
  <p>GenericDAO class</p>
  
  <pre><code>public class GenericDAOImpl&lt;T, ID extends Serializable&gt; implements
  GenericDAO&lt;T, ID&gt; {
  
  private EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
  private EntityManager em = emf.createEntityManager();
  
  protected EntityManager getEntityManager() {
      if (em == null) {
          throw new IllegalStateException(
                  "EntityManager has not been set on DAO before usage");           
      } else {        
          return em;
      }
  }
  
  public void refresh(T entity) {
      this.getEntityManager().refresh(entity);
  }
  ...
  }
  </code></pre>