• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

what is the diffrence between save() and saveOrUpdate() method in hibernate ?

Ranch Hand
Posts: 115
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the book of Cameron Mckenzie (Hibernate made easy) the saveOrUpdate() method is used to trace changes in session , if the changes in session before commit the transaction the saveOrUpdate() method trace the changes and commit according to the changes. page 166 . but the save method also do the same thing in page 179

for example
Posts: 1682
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
I have not read the book, and I am not quite following the question. As far as the difference between save and SaveOrUpdate,

save assumes a transient entity it will generate an insert statement and insert the Entity into the database it also returns immediately any generated key. I will quote from the documentation for the others

Usually update() or saveOrUpdate() are used in the following scenario:

the application loads an object in the first session

the object is passed up to the UI tier

some modifications are made to the object

the object is passed back down to the business logic tier

the application persists these modifications by calling update() in a second session

saveOrUpdate() does the following:

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 <version> or <timestamp>, and the version property value is the same value assigned to a newly instantiated object, save() it

otherwise update() the object

and merge() is very different:

if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance

if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance

the persistent instance is returned

the given instance does not become associated with the session, it remains detached
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic