org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
Problem I had two objects Contacts and Groups having many-to-many association. A Contact can belong to multiple Groups and a Group can contain multiple Contacts. My objective was to return a set of Contacts along with the Groups they were associated with. But if the Contacts-Groups association is set to Lazy load then the above exception will be thrown. Solution 1. FetchType can be set to EAGER and everything will work fine, the Groups will be loaded; but in a more general case, where the graph of java objects can grow very large, the use of eager fetch may cause unnecessary data to be loaded and the application will be slowed down. 2. Another solution is to explicitly load any lazy-loading the required fields actually hitting a method on those properties ex: contact.getEmailSet().size(). But this will result in subsequent sql firing which are required to load Groups (Child) Will also slow down the system. 3. Another solution is to include "fetch join" whi...