title: Hibernate, saving a collection property in a single operation
body: <p>I need a better understating of this behavior of hibernate, and wonder if I can get some light on the matter.
  There are two objects, <code>Contact</code> and <code>Action</code> with one to many relation, i.e. one <code>Contact</code> can have many <code>Action</code>s associated with them.
  What I am trying to understand is how can I store the collection of<code>Action</code>s (which is a property of <code>Contact</code>), when I store <code>Contact</code> to the database.
  At the moment what I am doing is that storing <code>Contact</code>s first and then storing <code>Action</code>s.
  The following is my code:
  Model Objects:</p>
  
  <pre><code>public class Contact  implements Serializable{
      private Integer contactID;
      private String givenName;
      private String familyName;
      private Set&lt;Action&gt; actionSet = new HashSet&lt;Action&gt;();
  }
  public class Action  implements Serializable{
      private Integer actionID;
      private String actionNote;
      private Contact contact;
  }
  </code></pre>
  
  <p>Hibernate Mapping:</p>
  
  <pre><code>&lt;hibernate-mapping package="com.hibernate.model" schema="hibernatedb"&gt;
  &lt;class name="Contact" table="CONTACT"&gt;
      &lt;id column="CONTACT_ID" length="500" name="contactID"&gt;
          &lt;generator class="increment" /&gt;
      &lt;/id&gt;
      &lt;property column="GIVEN_NAME" generated="never" lazy="false" length="100" name="givenName" /&gt;
      &lt;property column="FAMILY_NAME" generated="never" lazy="false" length="100" name="familyName" /&gt;
      &lt;!-- one to many mapping with Action --&gt;
      &lt;set inverse="true" lazy="false" name="actionSet" sort="unsorted"&gt;
          &lt;key column="CONTACT_ID" /&gt;
          &lt;one-to-many class="com.hibernate.model.Action" /&gt;
      &lt;/set&gt;
  
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
   &lt;hibernate-mapping package="com.hibernate.model" schema="hibernatedb"&gt;
  &lt;class name="Action" table="ACTION"&gt;
      &lt;id column="ACTION_ID" length="500" name="actionID"&gt;
          &lt;generator class="increment" /&gt;
      &lt;/id&gt;
      &lt;property column="ACTION_NOTE" type="string" name="actionNote" /&gt;
      &lt;!-- many to one mapping with Contact --&gt;
      &lt;many-to-one name="contact" column="CONTACT_ID"
          class="com.hibernate.model.Contact" lazy="false" cascade="save-update" /&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>This is how I am trying to store it at the moment:</p>
  
  <pre><code>public class ContactServiceImpl implements ContactService{
      @Override
      public void addContacts(Contact contact) {
  
          contactDAO.addContact(contact);//saving the contact;
          if((contact.getActionSet()!=null)&amp;&amp;(contact.getActionSet().size()&gt;0)){
              actionService.addAllActions(contact,contact.getActionSet());//saving actions, associated with the contact
          }
  
      }
  }
  </code></pre>
  
  <p>See, there are two operations leading to saving the <code>Action</code>s, where as they are a property of <code>Contact</code>, and I believe that when the <code>Contact</code> is saved, the collection of <code>Action</code>s must be saved as well.
  Please let me know the correct way of doing it. Thanks</p>