Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

hibernate

Through documentation i can find only one difference that is save method generates returns the object as generated identifier but persist does not.Is it the only purpose for providing persist method.If yes how does it help it to programmer becuase even if he does not intend to use generated identifier he can use save and ignore the return value.

Also came thru this thread at What's the advantage of persist() vs save() in Hibernate?. The meaningful statement i can get from this thread is persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries which save method does but not sure how should i try it in my programme so that i can get actual difference?

like image 893
M Sach Avatar asked Nov 11 '11 17:11

M Sach


1 Answers

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 .

like image 188
Deivanayagam Senthil Avatar answered Oct 03 '22 12:10

Deivanayagam Senthil