title: Foreign key not stored in child entity (one-to-many)
body: <p>I'm quite new to hibernate and have stumbled on this problem, which I can't find solution for.</p>
  
  <p>When persisting parent object (with <em>one-to-many</em> relationship with child), the foreign-key to this parent is not stored in child's table.</p>
  
  <p>My classes:</p>
  
  <h2>Parent.java</h2>
  
  <pre><code>@javax.persistence.Table(name = "PARENT")
  @Entity
  public class PARENT {
    private Integer id;
  
    @javax.persistence.Column(name = "ID")
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
      return id;
    }
  
    public void setId(Integer id) {
      this.id = id;
    }
  
    private Collection&lt;Child&gt; children;
  
    @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public Collection&lt;Child&gt; getChildren() {
      return children;
    }
  
    public void setChildren(Collection&lt;Child&gt; children) {
      this.children = children;
    }
  }
  </code></pre>
  
  <h2>Child.java</h2>
  
  <pre><code>@javax.persistence.Table(name = "CHILD")
  @Entity
  @IdClass(Child.ChildId.class)
  public class Child {
    private String childId1;
  
    @Id
    public String getChildId1() {
      return childId1;
    }
  
    public void setChildId1(String childId1) {
      this.childId1 = childId1;
    }
  
    private String childId2;
  
    @Id
    public String getChildId2() {
      return childId2;
    }
  
    public void setChildId2(String childId2) {
      this.childId2 = childId2;
    }
  
    private Parent parent;
  
    @ManyToOne
    @javax.persistence.JoinColumn(name = "PARENT_ID", referencedColumnName = "ID")
    public Parent getParent() {
      return parent;
    }
  
    public void setParent(Operation parent) {
      this.parent = parent;
    }
  
    public static class ChildId implements Serializable {
      private String childId1;
  
      @javax.persistence.Column(name = "CHILD_ID1")
      public String getChildId1() {
        return childId1;
      }
  
      public void setChildId1(String childId1) {
        this.childId1 = childId1;
      }
  
      private String childId2;
  
      @javax.persistence.Column(name = "CHIILD_ID2")
      public String getChildId2() {
        return childId2;
      }
  
      public void setChildId2(String childId2) {
        this.childId2 = childId2;
      }
  
      public ChildId() {
      }
  
      @Override
      public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
  
        ChildId that = (ChildId) o;
  
        if (childId1 != null ? !childId1.equals(that.childId1) : that.childId1 != null) 
          return false;
        if (childId2 != null ? !childId2.equals(that.childId2) : that.childId2 != null)
          return false;
  
        return true;
      }
  
      @Override
      public int hashCode() {
        int result = childId1 != null ? childId1.hashCode() : 0;  
        result = 31 * result + (childId2 != null ? childId2.hashCode() : 0);
        return result;
      }
    }
  }
  </code></pre>
  
  <h2>Test.java</h2>
  
  <pre><code>public class Test() {
  
    private ParentDao parentDao;
  
    public void setParentDao(ParentDao parentDao) {
      this.parentDao = parentDao;
    }
  
    private ChildDao childDao;
  
    public void setChildDao(ChildDao childDao) {
      this.childDao = parentDao;
    }
  
    test1() {
      Parent parent = new Parent();
  
      Child child = new Child();
      child.setChildId1("a");
      child.setChildId2("b");
      ArrayList&lt;Child&gt; children = new ArrayList&lt;Child&gt;();
      children.add(child);
  
      parent.setChildren(children);
      parent.setValue("value");
  
      parentDao.save(parent); //calls hibernate's currentSession.saveOrUpdate(entity)
    }
  
    test2() {
      Parent parent = new Parent();
      parent.setValue("value");
      parentDao.save(parent); //calls hibernate's currentSession.saveOrUpdate(entity)
  
      Child child = new Child();
      child.setChildId1("a");
      child.setChildId2("b");
      child.setParent(parent);
      childDao.save(); //calls hibernate's currentSession.saveOrUpdate(entity)
    }
  }
  </code></pre>
  
  <hr>
  
  <p>When calling <em>test1()</em>, both entities get written to database, but field PARENT_ID in CHILD table stays empty.</p>
  
  <p>The only workaround I have so far is <em>test2()</em> - persisting parent first, and then the child.</p>
  
  <p>My goal is to persist parent and its children in one call to <em>save()</em> on <em>Parent</em>.
  Any ideas?</p>