26

Normally I had read about save() method generates new identifier for object and only fire INSERT and save it, it does not update it, while saveOrUpdate() method may INSERT or UPDATE record.

But as per my experience, Here I can explains better by sample code,

Suppose there is Class A, and I find record from Table A by

A a = getHibernateTemplate.findById(7);

So now I get a persistent object,

And now I am trying to save record with save method by simply modifying some of fields,

Now I am firing,

getHibernateTemplate.save(a);

So it just update existing record, but as per my knowledge it should create new record.

I may be wrong about certian things, can someone clear about this?

commit
  • 4,777
  • 15
  • 43
  • 70
  • 4
    @GokcenG I'm executing a simple google search and came to this answer. Or are you saying that we should undertake a refactor to remove duplicate information from the itnernets? Also, since this was the first answer I saw under google it would be really nice if 5 of those annoying mod types hadn't tagged it a dupe--it's apparently got the most useful/accurate question name, at least for the search I used. Isn't that worth something? – Bill K Oct 08 '14 at 20:43

4 Answers4

27

save

Save method stores an object into the database. It will Persist the given transient instance, first assigning a generated identifier. It returns the id of the entity created.

Whereas,

SaveOrUpdate()

Calls either save() or update() on the basis of identifier exists or not. e.g if identifier exists, update() will be called or else save() will be called.

There are many more like persist(), merge(), saveOrUpdateCopy(). Almost all are same giving slight different functionality and usability.

For more, you can read this. What are the differences between the different saving methods in Hibernate?

Community
  • 1
  • 1
PVR
  • 2,534
  • 18
  • 38
  • I guess the main part of the question is not answered yet. If save is able t update the object, why is there a saveOrUpdate method. – Sumit Jul 26 '22 at 09:13
8

The important difference between the org.hibernate.Session class methods, save & saveOrUpdate is, save generates a new identifier and results in an INSERT query, whereas saveOrUpdate does an INSERT or an UPDATE.

save

Save method stores an object into the database. That means it insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

saveOrUpdate

This method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called. saveOrUpdate() method does the following:

If the object is already persistent in the current session, it do nothing If another object associated with the session has the same identifier, throw an exception to the caller If the object has no identifier property, save() the object If the object’s identifier has the value assigned to a newly instantiated object, save() the object

Read more from here.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
3

save() is supposed to take a transient instance as argument, and not a persistent instance. The javadoc is not very exhaustive, but AFAIK, calling save() on a persistent instance has no effect, other than cascading the operation to transient instances attached to the entity, is such a cascade is configured.

Note that there is usually no reason to call save() or saveOrUpdate() on an attached, persistent entity, since Hibernate dirty-checks the entities and makes persists the changes automatically.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

So it just update existing record, but as per my knowledge it should create new record: Yes it should create a new record. But this behavior is only for transient objects not for persistent object.

The moment you load the object by calling load, this object is associated with session(persistent state). And because of dirty checking, this object would have anyway been updated during flush.

But, if this object was a new object, it would have actually made a difference in calling save() or saveoOrUpdate() api.

ajay.patel
  • 1,957
  • 12
  • 15