Hibernate中persist()和save()之间的区别是什么? [英] Whats the difference between persist() and save() in Hibernate?

查看:114
本文介绍了Hibernate中persist()和save()之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过文档我可以发现只有一个区别是保存方法生成返回的对象作为生成的标识符但坚持不是。它是提供坚持方法的唯一目的。如果是的话它是如何帮助程序员,因为即使他不打算使用生成的标识符,他可以使用保存并忽略返回值。



还有通过这个线程在Hibernate中persist()vs save()有什么优势?。我可以从这个线程得到的有意义的声明是 persist()还保证,如果在保存方法的事务边界之外调用它,它将不会执行INSERT语句,但不知道该如何尝试它在我的程序中,我可以得到实际的区别?

解决方案

我做了一些模拟测试来记录Save()和Persist()。



听起来好像这两个方法在处理瞬态实体时表现相同,但在处理分离实体时表现不一样。



对于以下示例,将EmployeeVehicle作为实体与PK作为vehicleId,它是生成的值,并将vehicleName作为其属性之一。

使用Transient对象

  Session session = factory.openSession(); 
session.beginTransaction();
EmployeeVehicle实体=新的EmployeeVehicle();
entity.setVehicleName(Honda);
session.save(entity);
// session.persist(entity);
session.getTransaction()。commit();
session.close();

结果: select nextval('hibernate_sequence') //这是为车辆ID生成的:36

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

使用persist(entity)重复相同的操作,并且与新Id(例如37,honda)相同;

示例2:处理分离对象

  // Session 1 
//获取先前保存的车辆实体
Session session = factory.openSession();
session.beginTransaction();
EmployeeVehicle entity =(EmployeeVehicle)session.get(EmployeeVehicle.class,36);
session.close();

//会话2
//在会话2中,前一个会话中获取的车辆实体是分离的对象,现在我们将尝试保存/保存它
(i)使用Save()来持久分离的对象
Session session2 = factory.openSession();
session2.beginTransaction();
entity.setVehicleName(Toyota);
session2.save(entity);
session2.getTransaction()。commit();
session2.close();

结果:您可能希望以前会话中获得的ID为36的Vehicle更新为名称为丰田。但是会发生什么是一个新的实体被保存在数据库中,新的Id为Toyota生成并且名称为Toyota。

  select nextval ('hibernate_sequence')
插入Employee_Vehicle(Vehicle_Name,Vehicle_Id)值(Toyota,39)

(ii)使用Persist()来持久化分离的对象

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

//会话2
//在会话2中,前一个会话中获得的车辆实体是一个分离的对象,现在我们将尝试保存/持久化它
(i)使用persist()来坚持一个分离的对象

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

结果:引发异常:传递给持久化的分离实体



因此,使用Persist()而不是Save()作为保存会更好,因为在处理session和transcation时必须小心使用。


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?

解决方案

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 .

这篇关于Hibernate中persist()和save()之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆