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

前端 未结 6 727
孤独总比滥情好
孤独总比滥情好 2020-12-03 05:47

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

6条回答
  •  情歌与酒
    2020-12-03 06:26

    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 .

提交回复
热议问题