Wednesday, March 25, 2009

Hibernate:LazyInitializationException

Today I faced a strange error at office. I fixed the issue after a long struggle. But, the cause was really small,such that my manager could figure it within 5 minutes. Hibernate was throwing a error saying LazyInitializationError could not initialize collection. 

The cause of the problem was the call to hashCode method in the BaseDao class (Super class in this instance) that was trying to create a unique hashcode with all the elements of the persistent class (Base class in this Instance). The persistent class was having a Set of another Persistent class.  Example : X was consisting a Set of Y, Where the set of Y was pretty large and it consisted of another Set. So this process of creating hashCode from a set of set took a long time and created this problem.

Solution was to override the hashCode method in the Base class and create the hashCode using String Instance variables and avoiding the set's variables.

No comments: