Learnings from the Past: Hibernate related Best Practices

Back to blogging after a gap of 8 months. I have been working on some cool optimization stuff related to Hibernate. Here is a brief summary of the best practices i have come across on this and past assignments involving Hibernate.

# Practice What? Why? How?
1 Use Lazy Load Don’t load objects until you need to use them. Queries involve lesser joins, so become faster By Using @Lazy annotation for simple attributes and LazyCollection for Collections
2 Use allocationSize>1 for Sequences Use an allocationSize value of 50 or 100 instead of the default allocationSize of 1 Avoids frequent db access to fetch next sequence value By using @allocationSize=50, Hibernate will generate the next sequence values for 50 invocations before hitting the db again.
3 Keep big character/byte fields in a seperate entity Seperate big fields like blob, clob, varchar(500), etc., into a seperate entity called <enityName>Details Avoids time spent in fetching data for these fields on listing screens which do not typically display this information By using a one-to-one mapping with the details object and keeping the direction of association from main->detail
4 Use Hibernate Caching Using Hibernate Second Level cache and Query cache to store frequently used objects/results Avoids hitting the db for frequently used information Turn on Hibernate Second level cache and Quey cache

Tags: , , ,

Leave a Reply