如果我修改了一个Hibernate实体,在完成保存后,当我提交时会将这些更改保存到数据库中 [英] If I modify a Hibernate entity, after doing a save, when I commit would the changes be saved to the database

查看:273
本文介绍了如果我修改了一个Hibernate实体,在完成保存后,当我提交时会将这些更改保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在Hibernate中,我执行以下步骤:创建一个新的Hibernate POJO对象(即要保存的),并填入值。
  • 现在我执行session.save()。

  • >现在我更改POJO对象 - 说它是一个雇员对象,我做emp.setName(其他名称),即改变对象中雇员的名字。

  • 现在我做一个事务提交。

  • 我的问题是保存到数据库的内容 - 在执行session.save()或名称改变后,即其他名称?

    解决方案

    你保存实体的时刻,它变成了托管,并且所有进一步的变化都在会话刷新



    当您保存实体时,您只触发 EntityInsertAction 排队。在更改实体后,当前的实体状态已更改,因此在刷新期间,Hibernate将只插入最新的实体状态,因此数据库将包含其他名称。事实上,为一个托管实体调用一个像 save 这样的方法(它触发一个实体 merge )实际上会影响性能。查看这篇文章了解更多详情


    If in Hibernate, I do the following steps:

    1. Open a session.
    2. Create a new hibernate POJO object (that is to be saved), and fill in the values.
    3. Now I do session.save().
    4. Now I change the POJO object - say it is an employee object and I do emp.setName("Other Name") i.e change the name of the employee in the object.
    5. Now I do a transaction commit.

    My question is what would be saved to the database - the name before I did session.save() or the name after the change i.e. "Other Name"?

    解决方案

    The moment you saved the entity, it becomes managed and all further changes are propagated to the database during Session flushing.

    When you saved the entity, you only triggered an EntityInsertAction to be queued. After you changed the entity, the current entity state was changed so during a flush, Hibernate will simply insert the latest entity state, so the database will contain the "Other Name".

    In fact, calling a method like save for a managed entity (which triggers an entity merge) is actually going to affect performance. Check out this post for more details.

    这篇关于如果我修改了一个Hibernate实体,在完成保存后,当我提交时会将这些更改保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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