Reputation: 83
Hibernate session is not updating a column value of an entity if the column already has a value, if it is null 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:-
In my web app, I have, let's say, a Patient list. And the Patient entity has 3 columns -
Name | Age | Doctor
Please note that Doctor is a foreign key in the table
Now suppose from the list I am clicking on Assign button that will call an API which will only assign Doctor.
Now here is the catch, if a patient, say A does not have a Doctor present ( Or Assigned to A ) in DB and I click on Assign to set up new Doctor, the API will be called which will set Doctor for A, 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 (NULL). And if I try again it is setting up new Doctor value.
Here is the code of the API for updating/set email -
@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();
}
Thus the issue which I have been able to conclude is that if the Doctor column is null and API is called there will be no issue. But if there is value present in the Doctor column ( Foreign Key ), and the API is called the session is not updating the new value, instead it is setting value to null. I
Note: There is no error popping up in the code. Have tried by surrounding it with try/catch still no error. I have also tried replacing the session.save() to session.saveOrUpdate() but still no good.
Upvotes: 0
Views: 1763
Reputation: 83
so I figured out what was the issue. So the problem was that since two api are getting called - it is getting called simultaneously i.e asynchronous. So had to stop that , what I did in the API is disabled the asynchronous calling of APIs -
Ext.Ajax.request({
async: false,
url: webServiceUrl+'/setDoctor/',
method: 'POST',
params:{
So by giving async: false
in the API call it will prevent the parallel overlapping.
Upvotes: 0
Reputation: 690
Just append this lines before saving/updating,you need to begin a transaction before saving any data and then committing it.So that it must be in its critical state and no other transaction can update the data at same particular time (which can cause ambiguity)
session.beginTransaction();
session.saveOrUpdate(s);
Upvotes: 0