posted 12 years ago
Thanks Bill for your inputs.
I am trying to grasp the basics of following functions:
1. save()
2. saveOrUpdate()
3. update()
4. merge()
5. lock()
Considering the first method i.e. save() method, please refer the code snippet CODE SNIPPET I below.
I. I inserted some data in the DB with empId = 1 and then get this object in emp1 in session instance ssn1. Now, I called ssn1.save(emp1) but it doesn't save the object again into the db nor is any exception thrown. Now, if I open a new session, and then call ssn2.save(emp1), it inserts a new record into the db. Why it is behaving in this manner?
II. Also, I if I call saveOrUpdate(emp1) method on ssn2, then it doesn't insert a new record. Please explain this behavior.
CODE SNIPPET I
CODE SNIPPET II
Considering CODE SNIPPET II, I have the following queries:
saveOrUpdate: It either inserts a new record if one doesn't exist in db or updates the existing one if one with the given id exist in the db. But, how will it behave in case of detached objects?
update : It will reattach the object to the session. Suppose, I get an object say object1 using session1 and then session1 is closed, Now, the object1 is detached. And, I opened a new sessoin session2 and call update with object1 on this session session2, what will happen? What will happen if I call saveOrUpdate in this case rather than update method?
merge: what will happen if I call merge method rather than above two methods in above scenario.
Thanks for your patience and your time. I am really sorry if I am missing on some basics of the topic as I am a learner in this area.