title: Hibernate saving collections
body: <p>So Im writing a test application. I want to save collections within every collected class. Each collected class has a unieq list of Data. I cant save the data classes.</p>
  
  <p>the error I am getting
  ERROR: Referential integrity constraint violation: "FK2EEFAAF2485486: PUBLIC.DATA FOREIGN KEY(NUMBER) REFERENCES PUBLIC.COLLECTED(KEY) (4)"; SQL statement: insert into data (number, data) values (null, ?) [23506-170] </p>
  
  <p>Main class </p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package hibernatecollectiontest;
  
  import java.util.ArrayList;
  import java.util.List;
  import org.hibernate.Criteria;
  import org.hibernate.Session;
  
  /**
   *
   * @author ivan
   */
  public class HibernateCollectionTest {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) 
      {
          Session session = Util.getSessionFactory().openSession();
          Collected temp = new Collected();
          temp.setDescription("test");
          List&lt;Data&gt; local= new ArrayList&lt;&gt;(); 
          for(int i=0;i&lt;5;i++)
          {
              Data test = new Data();
              test.setData("Data"+i);
              local.add(test);
          }
          temp.setDatas(local);
          session.beginTransaction();
          session.save(temp);
          session.getTransaction().commit();
          Criteria crit = session.createCriteria(Collected.class);
          List&lt;Collected&gt; dump =crit.list();
          for(int i = 0 ; i &lt; dump.size();i++)
          {
              System.out.println(dump.get(i).getKey()+"  key "+dump.get(i).getDescription() +"  describe ");
              System.out.println("Size of collection is "+dump.get(i).getDatas().size());
              for(int n=0;n&lt;dump.get(i).getDatas().size();n++)
              {
                  System.out.println(dump.get(i).getDatas().get(n).getData());
              }
          }
          //session.flush();
      }
  }
  </code></pre>
  
  <p>The persistent class :</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package hibernatecollectiontest;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   *
   * @author ivan
   */
  public class Collected 
  {
      private String description;
      private int key;
      private List&lt;Data&gt; datas = new ArrayList&lt;&gt;();
  
      /**
       * @return the description
       */
      public String getDescription() {
          return description;
      }
  
      /**
       * @param description the description to set
       */
      public void setDescription(String description) {
          this.description = description;
      }
  
      /**
       * @return the key
       */
      public int getKey() {
          return key;
      }
  
      /**
       * @param key the key to set
       */
      public void setKey(int key) {
          this.key = key;
      }
  
      /**
       * @return the datas
       */
      public List&lt;Data&gt; getDatas() {
          return datas;
      }
  
      /**
       * @param datas the datas to set
       */
      public void setDatas(List&lt;Data&gt; datas) {
          this.datas = datas;
      }
  
  }
  </code></pre>
  
  <p>Data class inside the collection :</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package hibernatecollectiontest;
  
  /**
   *
   * @author ivan
   */
  public class Data 
  {
      private int number;
      private String data;
  
      /**
       * @return the number
       */
      public int getNumber() {
          return number;
      }
  
      /**
       * @param number the number to set
       */
      public void setNumber(int number) {
          this.number = number;
      }
  
      /**
       * @return the data
       */
      public String getData() {
          return data;
      }
  
      /**
       * @param data the data to set
       */
      public void setData(String data) {
          this.data = data;
      }
  }
  </code></pre>
  
  <p>Hibernate Mappings :</p>
  
  <pre><code>    &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
  &lt;property name="hibernate.connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
  &lt;property name="hibernate.connection.url"&gt;
  jdbc:h2:testdatabase
  &lt;/property&gt;
  &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
  &lt;property name="hibernate.connection.password"&gt;root&lt;/property&gt;
  &lt;property name="hibernate.connection.autocommit"&gt;true&lt;/property&gt;
  &lt;property name="show_sql"&gt;true&lt;/property&gt;
  &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
  &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
  &lt;!-- Mapping files --&gt;
  &lt;mapping resource="Collected.hbm.xml"/&gt;
  &lt;mapping resource="Data.hbm.xml"/&gt;
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Data.hbm</p>
  
  <pre><code>&lt;!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping package="hibernatecollectiontest"&gt;
  &lt;class name="Data" table="data"&gt;
  &lt;id name="number" column="number" type="java.lang.Integer"&gt;
  &lt;generator class="native"/&gt;
  &lt;/id&gt;
  &lt;property column="data" name="data" type="java.lang.String"/&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Collected.hbm</p>
  
  <pre><code>&lt;!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping package="hibernatecollectiontest"&gt;
  &lt;class name="Collected" table="collected"&gt;
  &lt;id name="key" column="key" type="java.lang.Integer"&gt;
  &lt;generator class="native"/&gt;
  &lt;/id&gt;
  &lt;property column="description" name="description" type="java.lang.String"/&gt;
  &lt;list name="datas" table="data" lazy="false" cascade="all"&gt;
  &lt;key column="number"/&gt;
  &lt;list-index column="sortOrder"/&gt;
  &lt;one-to-many class="hibernatecollectiontest.Data"/&gt;
  &lt;/list&gt;
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>