What Are the Differences Between the Different Saving Methods in Hibernate

What are the differences between the different saving methods in Hibernate?

Here's my understanding of the methods. Mainly these are based on the API though as I don't use all of these in practice.

saveOrUpdate
Calls either save or update depending on some checks. E.g. if no identifier exists, save is called. Otherwise update is called.

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.

update
Attempts to persist the entity using an existing identifier. If no identifier exists, I believe an exception is thrown.

saveOrUpdateCopy
This is deprecated and should no longer be used. Instead there is...

merge
Now this is where my knowledge starts to falter. The important thing here is the difference between transient, detached and persistent entities. For more info on the object states, take a look here. With save & update, you are dealing with persistent objects. They are linked to a Session so Hibernate knows what has changed. But when you have a transient object, there is no session involved. In these cases you need to use merge for updates and persist for saving.

persist
As mentioned above, this is used on transient objects. It does not return the generated ID.

Difference between save and saveOrUpdate method hibernate

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?

What's the difference between session.persist() and session.save() in Hibernate?

From this forum post

persist() is well defined. It makes a
transient instance persistent.
However, it doesn't guarantee that the
identifier value will be assigned to
the persistent instance immediately,
the assignment might happen at flush
time. The spec doesn't say that, which
is the problem I have with persist().

persist() also guarantees that it will
not execute an INSERT statement if it
is called outside of transaction
boundaries. This is useful in
long-running conversations with an
extended Session/persistence context.

A method like persist() is required.

save() does not guarantee the same, it
returns an identifier, and if an
INSERT has to be executed to get the
identifier (e.g. "identity" generator,
not "sequence"), this INSERT happens
immediately, no matter if you are
inside or outside of a transaction.
This is not good in a long-running
conversation with an extended
Session/persistence context.

Whats the difference between persist() and save() in Hibernate?

I did some mock testing to record the difference between Save() and Persist().

Sounds like both these methods behaves same when dealing with Transient Entity but differ when dealing with Detached Entity.

For the below example , take EmployeeVehicle as an Entity with PK as vehicleId which is a generated value and vehicleName as one of its property .

Example 1 : Dealing with Transient Object

                 Session session = factory.openSession();
session.beginTransaction();
EmployeeVehicle entity = new EmployeeVehicle();
entity.setVehicleName("Honda");
session.save(entity);
// session.persist(entity);
session.getTransaction().commit();
session.close();

Result : select nextval ('hibernate_sequence') // This is for vehicle Id generated : 36

insert into Employee_Vehicle ( Vehicle_Name, Vehicle_Id) values ( Honda, 36)

Repeat the same with using persist(entity) and will result the same with new Id ( say 37 , honda ) ;

Example 2 : Dealing with Detached Object

// Session 1 
// Get the previously saved Vehicle Entity
Session session = factory.openSession();
session.beginTransaction();
EmployeeVehicle entity = (EmployeeVehicle)session.get(EmployeeVehicle.class, 36);
session.close();

// Session 2
// Here in Session 2 , vehicle entity obtained in previous session is a detached object and now we will try to save / persist it
(i) Using Save() to persist a detached object
Session session2 = factory.openSession();
session2.beginTransaction();
entity.setVehicleName("Toyota");
session2.save(entity);
session2.getTransaction().commit();
session2.close();

Result : You might be expecting the Vehicle with id : 36 obtained in previous session is updated with name as "Toyota" . But what happens is that a new entity is saved in the DB with new Id generated for and Name as "Toyota"

         select nextval ('hibernate_sequence')
insert into Employee_Vehicle ( Vehicle_Name, Vehicle_Id) values ( Toyota, 39)

(ii) Using Persist() to persist a detached object

// Session 1
Session session = factory.openSession();
session.beginTransaction();
EmployeeVehicle entity = EmployeeVehicle)session.get(EmployeeVehicle.class, 36);
session.close();

// Session 2
// Here in Session 2 , vehicle entity obtained in previous session is a detached object and now we will try to save / persist it
(i) Using persist() to persist a detached object

            Session session2 = factory.openSession();
session2.beginTransaction();
entity.setVehicleName("Toyota");
session2.persist(entity);
session2.getTransaction().commit();
session2.close();

Result : Exception being thrown : detached entity passed to persist

So, it is always better to use Persist() rather than Save() as save has to be carefully used when dealing with session and transcation .

Differences among save, update, saveOrUpdate, merge methods in Session?

You are exactly right in all of your assessments. You get it.

For your first question, if i recall correctly, save specifically does an insert. So calling save again will result in another row in the db.

For your second question, update updates an object in the session. So if the object is in the session it will update. If the object is not in the session, you should call merge. I believe calling update for a detached instance will result in an exception.



Related Topics



Leave a reply



Submit