Wednesday, April 3, 2019

How to resolve LazyInitializationException in spring hibernate

lazyinitializationexception is thrown when




Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)

   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)

Solution:- 

we can use the static methods of hibernate. which provide the application with a convenient way to working with lazily initialized collections or proxies
1. Hibernate.initialize()
2. Hibernate.isInitialized()
 Hibernate.initialize(entity.getXXX()) will force the initialization of a proxy, entity.getXXX(), as long as its Session is still open. Hibernate.initialize( ) has a same effect for the collection of entities as well.

No comments:

Post a Comment