How does Hibernate detect dirty state of an entity object?

后端 未结 5 1984
一整个雨季
一整个雨季 2020-12-02 05:50

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 persiste

相关标签:
5条回答
  • 2020-12-02 06:32

    Hibernate does a field-by-field checking to determine the dirtiness of an entity.

    So hashCode/equals do not come into the picture at all.

    Actually, the field-by-field dirty checking done by Hibernate can be quite costly in terms of performance.

    So it provides interfaces like Strategy or Interceptor.findDirty() to handle the same.

    Following post explains this in greater detail (alongwith some ideas for applications to optimize it fully): http://prismoskills.appspot.com/lessons/Hibernate/Chapter_20_-_Dirty_checking.jsp

    0 讨论(0)
  • 2020-12-02 06:35

    does the dirty checking also involve any attached AttributeConverters? what if the value in the java object stays the same but the AttributeConverter logic is changed and does lead to different database values.

    so read entity with old AttributeConverter config, write entity with new AttributeConverter config.

    the java object stays the same for old and new AttributeConverter but the database values changes because of old and new AttributeConverter config.

    0 讨论(0)
  • 2020-12-02 06:46

    Hibernate default dirty checking mechanism will match all mapped properties of all currently attached entities against their initial loading-time values.

    You can better visualize this process in the following diagram:

    Default automatic dirty checking

    0 讨论(0)
  • 2020-12-02 06:53

    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.

    0 讨论(0)
  • 2020-12-02 06:54

    It is simple-- when you load/get entity object by id and then set its new field values by setter method and close session without calling update() method. then hibernate automatically update the changed value in the table without affecting other fields. and at the same time entity object is in dirty state.

    0 讨论(0)
提交回复
热议问题