title: Hibernate - automatically assign foreign key to child?
body: <p>Ok, so I have 2 objects, Parent and Child. The child basically looks like this:</p>
  
  <pre><code>public class Child implements Serializable 
  {
  
      // primary (composite) key
      private int parentId;
      private String name;
  
      // random value
      private String val;
  
      public Child(String name, String val) {
          this.name = name;
          this.val = val;
          // note: parentId is not assigned a value
      }
  
      public void setParentId(int id) {
  
      [...]
  }
  </code></pre>
  
  <p>What I want is basically for hibernate to automatically set the parent id of the Child once a Parent object is created. Is this possible or do I have to add child objects to the parent object after the parent object has been saved to the database?</p>
  
  <p>The xml mapping of the parent looks as such:</p>
  
  <pre><code>`&lt;map name="children" inverse="true" lazy="true" cascade="all,delete-orphan"&gt;
      &lt;cache usage="nonstrict-read-write"/&gt;
      &lt;key column="parent_id"/&gt;
      &lt;index column="child_name" type="string"/&gt;
      &lt;one-to-many class="myPack.Child"/&gt;
  &lt;/map&gt;`
  </code></pre>
  
  <p>and the xml of the child:</p>
  
  <pre><code>&lt;hibernate-mapping package="myPack"&gt;
  
      &lt;class name="Child" table="child_tbl" lazy="true"&gt;
  
          &lt;composite-id&gt;
              &lt;key-property name="ParentId" type="int" column="parent_id"/&gt;
              &lt;key-property name="Name" column="name" type="string"/&gt;
          &lt;/composite-id&gt;
  
          &lt;property name="Val" blablabla
  [...]
  </code></pre>