Sunday, October 22, 2006

Hibernate inner workings 1

Although I've been Hibernating for a couple of years now, it seems there is always more to know. I'm a big fan of hibernate as its allowed me to apply what I learned long ago in all my OO classes w/ CRC cards. It really is possible to put Domain Model Programming into practice. Okay, so why am I writing about it today? Because I finally looked at the source to see how it does change detection. Any object loaded by HIbernate, is "watched" by hibernate for changes. I knew this, but didn't really know how it works. So, from my brief look at the code, heres how it goes AFAIK

  1. You load objects via load, get, query, whatever
  2. Hibernate puts these objects in its session cache (a hashmap)
  3. Hibernate uses the key of the object and the class to as the key
  4. When sess.flush is called (either explicitly or because tx is finished or a query is executed)
  5. Hibernate goes throught its identitymap of object
    1. Check out the FlushEventListener class for details
  6. The EntityEntry for the object is found
  7. Dirty check takes place against the EntityEntry. Each EntityEntry has the loadedState of an object, which appears to be an array of simple properties
  8. Update is scheduled if necessary
I initialially thought hib did all this via CGLIB. Appears I was incorrect. Also, if you think your equals methods are involved in this evaluation, that appears to not be true either.

Cheers, long live hibernate


No comments: