• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Query about save(), saveorupdate(), update(), lock() and merge() methods

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am relatively new to Hibernate framework. I have written the following piece of code and changed the method calls to : save(), saveOrUpdate(), update(), lock() and merge() methods. Surprisingly, I am getting the same result by calling each and every method. The DB is updated with the newly set values in the Emp Objects. I am confused about the usage of these methods. I have gone through the Hibernate Community documentation as well but couldn't understand.



Thanks in advance.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of your questions can be answered by reading this thread:

http://stackoverflow.com/questions/161224/what-are-the-differences-between-the-different-saving-methods-in-hibernate

lock(emp1, LockMode.NONE) was used to reassociate a transient instance with a session. If you call update on a detached object it triggers an update whether the object has changed or not. To avoid this lock was used to re associate the instance with a session so this would not happen. Note that lock is now deprecated.

I prefer to just use JPA2
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In your example update saveOrUpdate and merge behave the same but merge is very different. Please read:
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#objectstate-saveorupdate
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill for your patience and time. I have gone through the link provided but still things are not very much clear to me. Can you please update my sample code so that it produces different results for merge, saveOrUpdate and lock methods and I can visualize the difference among these methods. Thanks again for your time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic