title: Hibernate - One to many relation - foreign key always "null"
body: <p>I have two really simple objects and one object should contain the other one in an "one-to-many" relation in a set. The objects get inserted correctly in the database, but the foreign key in the "children" table is always "null".</p>
  
  <p>I can't figure out why:</p>
  
  <p>This is the test-object and it hold the children in its set:</p>
  
  <pre><code>@Entity
  @Table(name="test")
  public class TestObj {
  
      public TestObj(){}
  
      private Long id;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      public Long getId() {
          return id;
      }
      public void setId(Long id) {
          this.id = id;
      }
  
      private Set&lt;Children&gt; children = new HashSet&lt;Children&gt;();
  
      @OneToMany(mappedBy = "testObj", cascade = CascadeType.ALL)
      public synchronized Set&lt;Children&gt; getChildren() {
          return children;
      }
      public synchronized void setChildren(Set&lt;Children&gt; children) {
          this.children = children;
      }
      public void addChildren(Children child){
          children.add(child);
      }
  }
  </code></pre>
  
  <p>This is the children object and it holds an back-link to the "TestObj":</p>
  
  <pre><code>@Entity
  @Table(name = "children")
  public class Children {
  
      public Children(){}
  
      private Long id;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      private TestObj testObj;
  
      @ManyToOne
      @JoinColumn
      public TestObj getTestObj() {
          return testObj;
      }
  
      public void setTestObj(TestObj testObj) {
          this.testObj = testObj;
      }
  }
  </code></pre>
  
  <p>I persist this objects with this code:</p>
  
  <pre><code>EntityManagerFactory entityManagerFactory = HibernateEntityMangerSingelton.getEntityManagerFactory();
  EntityManager entityManager = entityManagerFactory.createEntityManager();
  entityManager.getTransaction().begin();
  
  
  TestObj user = new TestObj();
  
  Children child = new Children();
  user.addChildren(child);
  try {
  
      entityManager.persist(user);
  
      entityManager.getTransaction().commit();
  
  } catch (Exception e) {
      System.out.println(e);
  }finally{
      entityManager.close();
  }
  </code></pre>
  
  <p>Can some explain me why this is happening?</p>