title: One-To-Many - How to Link to Objects correctly?
body: <p>I'm using JPA and have some difficulties to understand how the One-To-Many Realtionship works.</p>
  
  <p>I have the following two classes:</p>
  
  <pre><code>@Entity
  public class myCheck {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      protected int Check_id;
  
      @Column
      private String name;
  
      @ManyToOne
      private mySystem system;
  
      @Override
      public String toString() {
          return this.name;
      }
  
      public int getId() {
          return Check_id;
      }
  
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public mySystem getLinkSystem() {
          return system;
      }
  
      public void linkSystem(mySystem system) {
          this.system = system;
      }
  
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>@Entity
  public class mySystem {
      @Id
      @GeneratedValue
      @Column(name = "system_id")
      protected int system_id;
      @Column
      public String name;
  
      @ManyToOne(cascade=CascadeType.ALL)
      private mySystem parent;
  
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "system", fetch = FetchType.EAGER)
      private List&lt;myCheck&gt; checks;
  
      public mySystem() {
          //subSystems = new ArrayList&lt;mySystem&gt;();
          checks = new ArrayList&lt;myCheck&gt;();
      }   
  
      public boolean linkCheck(myCheck hc) {
          return checks.add(hc);
      }
  
      public boolean unlinkCheck(myCheck hc) {
          return checks.remove(hc);
      }
  
      public List&lt;myCheck&gt; getlinkedChecks() {
          return checks;
      }
  
      public myCheck getLinkCheck(int hcId) {
          for (myCheck hc : checks) {
              if (hc.getId() == hcId)
                  return hc;
          }
          return null;
      }
  
      public int getId() {
          return system_id;
      }
  
      public void setId(int id) {
          this.system_id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      @Override
      public String toString() {
          return this.getName();
      }
  
  }
  </code></pre>
  
  <p>Now I have an existing System in my database, which is loaded:</p>
  
  <pre><code>    // load System
      EntityManager entityManager1 = entityManagerFactory.createEntityManager();
      List&lt;mySystem&gt; systems = entityManager1.createQuery("from mySystem").getResultList();
      entityManager1.close();
  </code></pre>
  
  <p>I want to add two new checks to Systems. What is working is:</p>
  
  <pre><code>    EntityManager entityManager2 = entityManagerFactory.createEntityManager();
      entityManager2.getTransaction().begin();
  
      myCheck check = new myCheck();
      check.setName("Check 1");
      check.linkSystem(systems.get(0));
      entityManager2.persist(check);
  
      myCheck check2 = new myCheck();
      check2.setName("Check 2");
      check2.linkSystem(systems.get(0));
      entityManager2.persist(check2);
  
      entityManager2.merge(systems.get(0));
      entityManager2.getTransaction().commit();
      entityManager2.close();
  </code></pre>
  
  <p>But I can't do this:</p>
  
  <pre><code>    EntityManager entityManager2 = entityManagerFactory.createEntityManager();
      entityManager2.getTransaction().begin();
  
      myCheck check = new myCheck();
      check.setName("Check 1");
      systems.get(0).linkCheck(check);
      entityManager2.persist(check);
  
      myCheck check2 = new myCheck();
      check2.setName("Check 2");
      systems.get(0).linkCheck(check);
      entityManager2.persist(check2);
  
      entityManager2.merge(systems.get(0));
      entityManager2.getTransaction().commit();
      entityManager2.close();
  </code></pre>
  
  <p>The second solution will save the checks, but I don't link them with the system. </p>
  
  <p>Has someone a explanation for this? I really want to understand this.</p>