Wednesday, September 21, 2011

My 2 cents on Hibernate

 Few hibernate experiences -

1. Implementing hashcode and equals on hibernate entities -
    Remember hibernate returns a set of entities when you have a one to many or a many to many relationships.
Accoring to Set rules - no two objects can be equal and hence it becomes all the more important to define the right equals implementation for your hibernate POJO's
Rule to thumb - equals should return true if the primary keys of the entities match.

2. Hibernate has dual caching layer -
    First level cache is stored at the hibernate SessionFactory level and the second level cache could be(if you choose to) a third party caching library.
Remember - enable second level cache only if your application has a high read to write ratio.

3. Hibernate transaction management -
    I am assuming in most cases you would use JTA. Hibernate sessions are stored on the transaction and hence if you use threadlocal to create and manage hibernate session ensure you clear the thread local once the session usage is over. Without this done explicity there is every chance that the session would not be garbage collected unless the transaction context is. Generally you would run into this issue if a transaction for some reason cannot be commited or rolledback and runs into a timeout.

No comments: