Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?

后端 未结 6 445
太阳男子
太阳男子 2020-12-29 11:31

I am attempting to create a web application using Spring MVC, with Hibernate as its ORM layer. However, due to my inexperience with both frameworks I\'m struggling.

6条回答
  •  余生分开走
    2020-12-29 11:45

    The Hibernate.initialize(list) call doesn't initialize the target entity objects that are referenced in the collection. You would need to iterate over reports and initialize each individual object. The call to initialize reports turns a proxy collection into a concrete collection of Report proxies. Try code below:

    for(Report r : reports)
       Hibernate.initialize(r);
    

    The blunt axe approach is to turn off lazy loading by adding lazy="false" to your hbm classes. This might make sense if you will always iterate over the entire object every time its retrieved (make the initialization step OBE).

提交回复
热议问题