Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does org.hibernate.Session.save() do?

Tags:

I know that Session.save() persists the transient object. And I see that it also has saveOrUpdate(), and also persist().

I suppose then that save() is equivalent to SQL INCLUDE, is it?

If I have an object that already exists on DB and I save() it, will another row be included, will its fields be updated, or will it just be ignored and nothing happen?

like image 990
Hikari Avatar asked Mar 05 '13 14:03

Hikari


People also ask

What is session save in hibernate?

Hibernate save method returns the generated id immediately, this is possible because primary object is saved as soon as save method is invoked. If there are other objects mapped from the primary object, they gets saved at the time of committing transaction or when we flush the session.

What is the use of the save () method?

save() method is executed whenever an instance of a particular model is created either from admin interface or shell, save() method is run. You can override save() function before storing the data in the database to add new data or remove some data to be stored in a database.

What is the difference between the Save () and saveOrUpdate () method of hibernate?

Difference between save and saveOrUpdate in Hibernatesave() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.

What is the difference between session Save () and session persist () Me?

Difference between saving and persist method in Hibernate 1)The first difference between save and persist is there return type. Similar to save method, persist also INSERT records into the database, but return type of persist is void while return type of save is Serializable Object.


2 Answers

Difference between save and saveOrUpdate

Main difference between save and saveOrUpdate method is that save generates a new identifier and INSERT record into database while saveOrUpdate can either INSERT or UPDATE based upon existence of record. So save will proceed without performing existence check, on the other hand saveOrUpdate will check for existence, if record exists it will be updated else a new record will be inserted.

Basic differences between persist and save

1)First difference between save and persist is their return type. Similar to save method, persist also INSERT records into database but return type of persist is void while return type of save is Serializable object.

2) Another difference between persist and save is that both methods make a transient instance persistent. However, persist method doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.

like image 194
D3V Avatar answered Oct 03 '22 22:10

D3V


     save Persists an entity. Will assign an identifier if one doesn't exist.      If one does, it's essentially doing an update.       Returns the generated ID of the entity. 

I'm suggesting, You really need to read this for proceeding further.

like image 21
Suresh Atta Avatar answered Oct 04 '22 00:10

Suresh Atta