Hibernate如何检测实体对象的脏状态? [英] How does Hibernate detect dirty state of an entity object?

查看:129
本文介绍了Hibernate如何检测实体对象的脏状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



或者,也许Hibernate通过将给定的对象与以前的持久版本进行比较来获取脏状态?



我遇到了 hashCode() equals() code>复杂对象的方法。如果对象具有集合成员,那么计算哈希代码的速度会很慢,而循环引用也是一个问题。



如果Hibernate不会使用 hashCode() / equals()来检查脏状态,我想我不应该使用 equals ) / hashCode()为实体对象(不是值对象),但是我也害怕如果相同的操作符(<$ c $因此,问题是:

$ b

ol>
  • Hibernate如何知道对象的属性是否发生了变化?

  • 你建议覆盖 hashCode() / equals()复杂对象的方法吗?如果他们包含循环引用呢?



    此外, 只有 id 字段的hashCode() / equals()是否足够? / p>


    解决方案

    Hibernate使用名为检查的策略,这基本上是这样的:从数据库加载对象时,其快照保存在内存中。当会话刷新时,Hibernate将存储的快照与当前状态进行比较。如果它们不同,则该对象被标记为脏并且合适的SQL命令被入队。如果对象仍然是短暂的,那么它总是很脏。



    来源:book Hibernate in Action(附录B:ORM实现策略)

    然而,重要的是要注意Hibernate的脏检查与方法equals / hascode 无关。 Hibernate根本没有查看这些方法(除了使用java.util.Set之外,但这与脏检查无关,仅适用于Collections API)。前面提到的状态快照与一组值类似。将这个框架的核心部分留在开发者手中是一个非常糟糕的决定(老实说,开发人员不应该关心肮脏检查)。不用说,equals / hascode可以根据您的需要以多种方式实现。我建议你阅读引用的书,那里的作者讨论等于/ hascode实施策略。非常有洞察力的阅读。


    Is it using some kind of byte codes modification to the original classes?

    Or, maybe Hibernate get the dirty state by compare the given object with previously persisted version?

    I'm having a problem with hashCode() and equals() methods for complicated objects. I feel it would be very slow to compute hash code if the object has collection members, and cyclic references are also a problem.

    If Hibernate won't use hashCode()/equals() to check the dirty state, I guess I should not use equals()/hashCode() for the entity object (not value object), but I'm also afraid if the same operator (==) is not enough.

    So, the questions are:

    1. How does Hibernate know if a property of an object is changed?

    2. Do you suggest to override the hashCode()/equals() methods for complicated objects? What if they contains cyclic references?

      And, also,

    3. Would hashCode()/equals() with only the id field be enough?

    解决方案

    Hibernate uses a strategy called inspection, which is basically this: when an object is loaded from the database a snapshot of it is kept in memory. When the session is flushed Hibernate compares the stored snapshot with the current state. If they differ the object is marked as dirty and a suitable SQL command is enqueued. If the object is still transient then it is always dirty.

    Source: book Hibernate in Action (appendix B: ORM implementation strategies)

    It's important to notice however that Hibernate's dirty-checking is independent of the methods equals/hascode. Hibernate does not look at these methods at all (except when using java.util.Set's, but this is unrelated to dirty-checking, only to the Collections API) The state snapshot I mentioned earlier is something similar to an array of values. It would be a very bad decision to leave such a core aspect of the framework in the hands of developers (to be honest, developers should not care about dirty-checking). Needless to say that equals/hascode can be implemented in many ways according to your needs. I recommend you to read the cited book, there the author discuss equals/hascode implementation strategies. Very insightful reading.

    这篇关于Hibernate如何检测实体对象的脏状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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