休眠:脏检查和只更新脏属性? [英] Hibernate: Dirty Checking and Only Update of Dirty Attributes?

查看:71
本文介绍了休眠:脏检查和只更新脏属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在古老的JDBC时代中,我写了很多SQL代码,这些代码只针对实际更改的属性/成员进行了非常有针对性的更新:

例如,考虑具有以下成员的对象:

  public String name; 
公共字符串地址;
public日期日期;

如果只有日期方法我只会为日期成员发出SQL UPDATE


$ b $然而,看起来(这是我对Hibernate的印象),当使用标准Hibernate映射(映射完整类)时,即使只更新了一个成员,也会导致SQL语句中对象的完全更新由Hibernate生成。



我的问题是:


  1. 观察正确,Hibernate 不会智能地检查(在完全映射的类中)哪些成员发生了更改,然后只针对特定的更改成员发布更新,而总是会更新(在生成的SQL更新语句)所有映射的成员(类的),即使它们没有改变(如果对象因一个成员脏而变脏...)

  2. li>

    我能做些什么来让Hibernate只更新那些成员,那就是hav被改变了吗?我正在寻找一种解决方案,让Hibernate只更新实际改变的成员。 一些关于脏检查的工作,但据我所知,这种脏检查仅与识别整个对象是否脏,而不是什么单个成员是脏的有关。)

    dynamic-update 和 dynamic-insert 此处。


    In the "good old JDBC days" I wrote a lot of SQL code that did very targeted updates of only the "attributes/members" that were actually changed:

    For example, consider an object with the following members:

    public String name;
    public String address;
    public Date date;
    

    If only date was changed in some Business Method I would only issue an SQL UPDATE for the date member.

    It seems however (that's my "impression" of Hibernate) that when working with a standard Hibernate mapping (mapping the full class), even updates of only a single member lead to a full update of the object in the SQL statements generated by Hibernate.

    My questions are:

    1. Is this observation correct, that Hibernate does not intelligently check (in a fully mapped class), what member(s) where changed and then only issue updates for the specific changed members, but rather always will update (in the generated SQL Update Statement) all mapped members (of a class), even if they were not changed (in case the object is dirty due to one member being dirty...)

    2. What can I do to make Hibernate only update those members, that have been changed? I am searching for a solution to have Hibernate only update the member that actually changed.

    (I know Hibernate does quite some work on dirty-checking, but as far as I know this dirty checking is only relevant to identify if the object as whole is dirty, not what single member is dirty.)

    解决方案

    Actually, you can specify the options dynamic-update and dynamic-insert in a class mapping. It does just that. More info here.

    这篇关于休眠:脏检查和只更新脏属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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