title: Understand OneToMany Relationship in JPA
body: <p>I have two entities in in OneToMany Relationship:</p>
  
  <p>The parent Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "PIANOTAGLIE")
  public class PianoTaglia {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String pianoTaglia;
  
      @OneToMany(fetch = FetchType.EAGER, mappedBy = "pianoTaglia", cascade = CascadeType.ALL)
      private List&lt;Taglia&gt; taglie;
  
      public PianoTaglia() {
      }
  
      [...] Getter/Setter [...]
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((id == null) ? 0 : id.hashCode());
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          PianoTaglia other = (PianoTaglia) obj;
          if (id == null) {
              if (other.id != null)
                  return false;
          } else if (!id.equals(other.id))
              return false;
          return true;
      }
  }
  </code></pre>
  
  <p>And Child entity:</p>
  
  <pre><code>@Entity
  @Table(name = "TAGLIE")
  public class Taglia {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(length = 10, unique = true)
      private String taglia;
  
      @ManyToOne
      private PianoTaglia pianoTaglia;
  
      public Taglia() {
      }
  
      [...] Getter/Setter [...]
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((id == null) ? 0 : id.hashCode());
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          Taglia other = (Taglia) obj;
          if (id == null) {
              if (other.id != null)
                  return false;
          } else if (!id.equals(other.id))
              return false;
          return true;
      }
  }
  </code></pre>
  
  <p>For manage my Entities i use this generic Dao:</p>
  
  <pre><code>public abstract class JpaDAO&lt;E&gt; {
  
      protected Class&lt;E&gt; entityClass;
  
      @PersistenceContext(unitName = "PrudiPU")
      protected EntityManager em;
  
      @SuppressWarnings("unchecked")
      public JpaDAO() {
          ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
          this.entityClass = (Class&lt;E&gt;) genericSuperclass.getActualTypeArguments()[0];
      }
  
      public List&lt;E&gt; findAll() {
          TypedQuery&lt;E&gt; q = em.createQuery("SELECT h FROM " + entityClass.getName() + " h", entityClass);
          return q.getResultList();
      }
  
      public void persist(E entity) {
          em.persist(entity);
      }
  
      public E getReference(Long id) {
          return em.getReference(entityClass, id);
      }      
  }
  </code></pre>
  
  <p>Specialized for each class (this is PianoTagliaDao but TagliaDao is the same)</p>
  
  <pre><code>@Repository
  public class PianoTaglieDao extends JpaDAO&lt;PianoTaglia&gt; {
  
  }
  </code></pre>
  
  <p>When I create a PianoTaglia I keep a reference to the object with the generated ID... So i can navigate through my application and at any time i can create a Taglia. When i create a Taglia i use the reference to PianoTaglia, previusly created, in this way:</p>
  
  <pre><code>PianoTaglia pt = getPreviuslyCreatedPianoTaglia(); //this is an example
  Taglia tg = new Taglia();
  tg.setTaglia("XXL");
  tg.setPianoTaglia(pt);
  pt.getTaglie().add(tg);
  taglieDao.persist(tg);
  taglieDao.flush(); //i need to flush for keep generated ID
  [...]
  </code></pre>
  
  <p>If i check the tables into DB is all ok! All the tables are well populated! But if i try to get all PianoTaglia the taglie collections are always empty:</p>
  
  <pre><code>List&lt;PianoTaglia&gt; pianoTagle = pianoTagliaDao.findAll();
  for(PianoTaglia pt : pianoTaglie) {
      assert pt.getTaglie().isEmpty();
  }
  </code></pre>
  
  <p>after testing i've found the solution: when i create taglia i have to keep a new reference of PianoTaglie:</p>
  
  <pre><code>PianoTaglia old = getPreviuslyCreatedPianoTaglia();
  PianoTaglia pt = pianoTaglieDao.getReference(old.getId()); //getReference call the namesake method in the EntityManager
  Taglia tg = new Taglia();
  tg.setTaglia("XXL");
  tg.setPianoTaglia(pt);
  pt.getTaglie().add(tg);
  taglieDao.persist(tg);
  taglieDao.flush(); //i need to flush for keep generated ID
  [...]
  </code></pre>
  
  <p>In this way when i keep the PianoTaglia Objects the taglie collections are well Populated..</p>
  
  <p>My question is: Why JPA have this behaviour?</p>