Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Hibernate detect dirty state of an entity object?

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?

like image 433
Xiè Jìléi Avatar asked Mar 11 '11 02:03

Xiè Jìléi


People also ask

How does Hibernate check dirty?

Hibernate provides as feature called Automatic Dirty checking whereby changes to a persistent object are automatically saved to the database when the session is flushed or the transaction is committed. So the code does not need to invoke an explicit save or update.

How do you stop dirty checking in Hibernate?

A solution to this problem is to change the default configuration of FlushMode from auto to manual by setting FlushMode. MANUAL . In this way the dirty check mechanism will stop causing the aforementioned synchronization. Although the Session is only ever flushed when Session.

What is dirty checking in Hibernate Mcq?

Hibernate automatically detects object state changes in order to synchronize the updated state with the database, this is called dirty checking.

What are the entity states in Hibernate?

In the Hibernate framework, an entity can be in three states, transient, persistent, and detached.


1 Answers

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.

like image 101
Antonio Avatar answered Sep 28 '22 06:09

Antonio