aspose file tools*
The moose likes Object Relational Mapping and the fly likes what is the diffrence between save() and saveOrUpdate() method in hibernate ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "what is the diffrence between save() and saveOrUpdate() method in hibernate ?" Watch "what is the diffrence between save() and saveOrUpdate() method in hibernate ?" New topic
Author

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

Singh Harmeet
Ranch Hand

Joined: Aug 05, 2011
Posts: 115

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


With Regards :-
Harmeet Singh
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

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


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the diffrence between save() and saveOrUpdate() method in hibernate ?