This is caused because Hibernate, by default, lazily loads your model objects for performance reasons. However, it can only load data from the database into your model while a Hibernate session is active. Generally, in Spring, the Hibernate session is controlled by transaction boundaries. In this case you can't really extend the transaction boundary because you're first accessing the collection in a JSP. There are a couple of other things you can do to overcome this - you can turn off lazy loading for that collection in your hibernate configuration (in your .hbm or in annotations) by setting fetch=FetchType.EAGER, or you can create a OpenSessionInViewFilter to keep the Hibernate session open in your JSPs. Both of these approaches may cause issues down the road - by setting FetchType.EAGER, all subcategories for all categories are populated every time - but if there just aren't that many of them, this isn't a problem. By creating an OpenSessionInViewFilter you can now do database calls in your view layer, and you may inadvertently do something that is computationally expensive, and receive no warning - like the exception here.
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
subject: How to deal with List in jstl using forEach