title: Hibernate Session not updating entity
body: <p>Hibernate session is not updating a column value of an entity if the column already has a value, if it is <strong>null</strong> then no problem, the session is saving the new update. However if the entity's column already has a value and I am updating the column ( Not by HSQL query, but by setter ) the session is not updating the value in the database.
  For Example:-</p>
  
  <p>In my web app, I have, let's say, a Patient list. And the Patient entity has 3 columns -</p>
  
  <blockquote>
    <p>Name |  Age | Doctor</p>
  </blockquote>
  
  <p><strong>Please note that <em>Doctor</em> is a foreign key in the table</strong><br>
  Now suppose from the list I am clicking on <strong>Assign</strong> button that will call an API which will only assign Doctor.</p>
  
  <blockquote>
    <p>Now here is the catch, if a patient, say <strong>A</strong> does not have a Doctor present ( Or Assigned to A ) in DB and I click on <em>Assign</em> to set up new Doctor, the API will be called which will set Doctor for <strong>A</strong>, which will work fine without any problem. But now let's say I want to update the assigned Doctor to a new one. So I would again click on button thus again API will be called, only this time the new Doctor is not getting updated in DB, instead the Doctor column remains <strong>(NULL)</strong>. And if I try again it is setting up new Doctor value.</p>
  </blockquote>
  
  <p>Here is the code of the API for updating/set email -</p>
  
  <pre><code> @POST
   @Path("/setDoctor/")
   @Produces(MediaType.APPLICATION_JSON)
   public JSONObject setDoctor(
      @FormParam('patientId') int patientId,
      @FormParam('DoctorId') int doctorId ){
  Session session = HibernateSessionFactory.get().openSession();
  session.beginTransaction();
  Patient patient = (Patient) session.get(Patient.class, patientId);
  patient.setDoctor((Doctor) session.get(Doctor.class,doctorId));
  session.save(patient);
  session.getTransaction().commit();
  session.close();
  }
  </code></pre>
  
  <p>Thus the issue which I have been able to conclude is that if the Doctor column is <strong>null</strong> and API is called there will be no issue. But if there is value present in the Doctor column ( <em>Foreign Key</em> ), and the API is called the session is not updating the new value, instead it is setting value to <strong>null</strong>. I</p>
  
  <p><strong>Note:</strong>
  There is no error popping up in the code. Have tried by surrounding it with <strong>try/catch</strong> still no error. I have also tried replacing the <strong>session.save()</strong> to <strong>session.saveOrUpdate()</strong> but still no good.</p>