2

I am familiar with Hibernate.I have a question.

Lets say I do session.saveOrUpdate(object). If object contains some 'id'(object identifier), then it means update query will be fired if the object is modified.

I want to know how does Hibernate do that i.e. there could be 'n' number of fields in the object.Does Hibernate check each and every field to know if any field is modified(in this case update query to be fired)?

0

3 Answers 3

2

While using use .saveOrUpdate() it will check if the object has no identifier property and if so it will make it persistent by generating it the identifier and assigning it to session.

if the object is already persistent in this session, do nothing if another object associated with the session has the same identifier, throw an exception if the object has no identifier property, save() it if the object's identifier has the value assigned to a newly instantiated object, save() it if the object is versioned by a or , and the version property value is the same value assigned to a newly instantiated object, save() it otherwise update() the object

From Hibernate Docs

Sign up to request clarification or add additional context in comments.

7 Comments

so even if no field is modified, still update query will be fired?
yes it will fire.Because it will check only the identifier field
ok..i thought that if no field is modified, update will not be fired. Isn't it a performance issue?? i mean if no change is there in the field, then why to fire update query..
In my knowledge it only checks the id.You are right in this case .Updation is not necessary.But it is not working like that.You can observe by putting show_sql to true
The nHibernate Session keeps track of what needs writing to the database, if you commit a transaction it only updates objects that have changed or inserts new objects.
|
0

It checks the field(s) mapped as the identifier for the given object, if the object doesn't have an identifier it calls Save() which INSERTS the object, if it does it calls Update() which UPDATES it

Comments

0

Basically in hibernate if you set object with unique identifier any how it is going to update the whole row by calling session.saveOrUpdate();

or if unique identifier not found it insert the data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.