title: saveorupdate() does not update collection (list) one-to-many mapping in hibernate
body: <p>class <code>Student</code> </p>
  
  <pre><code>public class Student {
  
  private Long id;
  private String name;
  private String className;
  private List&lt;Phone&gt; phones;
      // getter setter
   }
  </code></pre>
  
  <hr>
  
  <p>class <code>Phone</code></p>
  
  <pre><code>public class Phone {
  
  private Long id;
  private String number;
     //getter setter
       }
  </code></pre>
  
  <p>-> mappping file <code>Student.hbm.xml</code></p>
  
  <p></p>
  
  <pre><code>     &lt;id name="id" type="long" column="id"&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
  
      &lt;property name="name" column="name" type="string" /&gt;
  
      &lt;property name="className" column="class_name" type="string" /&gt;
  
      &lt;list name="phones" cascade="all-delete-orphan"&gt;
          &lt;key column="student_id"/&gt;
          &lt;list-index column="idx" /&gt;
          &lt;one-to-many class="Phone" /&gt;
      &lt;/list&gt;
  </code></pre>
  
  <p>-> mapping file Phone.hbm.xml</p>
  
  <pre><code>    &lt;id name="id" type="long" column="id"&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
  
      &lt;property name="number" column="number" type="string" /&gt;
  </code></pre>
  
  <p>when i try to update the the phone number (a list), the previous entry is not deleted but the idx(list index) and the foreign key are null and the new entries are marked with the correct idx and foreign key!!The other data (not a list) is updated completely fine. Here I get the class object from the database, modify that and pass the object to <code>saveorupdate()</code>, but didn't help. Been trying this so long.</p>
  
  <p>Code to read and update: </p>
  
  <pre><code>      private void readAndUpdateStudent() {
         Student student = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         String name = "John";
           try {
  
          String queryString = "from Student student where student.name =:name";
          Query query = session.createQuery(queryString);
          query.setString("name", name);
          student = (Student) query.uniqueResult();
          System.out.println(student.getName());
          student.setName("Mary");
          List&lt;Phone&gt; phones = new ArrayList&lt;Phone&gt;();
          phones.add(new Phone("555555")); //here if I SET rather adding, the already existing values, update is done accordingly 
          phones.add(new Phone("789789"));//but when I use the list as a string type(instead of Phone Type), adding a new 0bject deletes the previous 
          student.setPhones(phones);//entries and adds new list on its own  but not in the case of user defined type.
  
      } catch (Exception e) {     
          e.printStackTrace();
          if (session != null) {
  
              session.close();
  
          }
  
      }finally{
  
          session.close();
          updateStudent(student);
      }
  
  }
  private void updateStudent(Student student) {
  
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = null;
  
      try {
          tx = session.beginTransaction();
          session.saveOrUpdate(student);
          tx.commit();
      } catch (HibernateException e) {
          if (tx != null)
              tx.rollback();
          e.printStackTrace();
      } finally {
          session.close();
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/huyqE.png" alt="enter image description here"></p>