Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to deal with List in jstl using forEach

 
soonphei tin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I have a Category and SubCategory classes. Obviously, they are one-to-many relationship between Category and SubCategory. Below is the code fragment: -




I have populated the data and if Category is set as CommandClass as below........


When I refer to SubCategory using jstl within JSP like below....


I get JasperException with the below message: -


Has anyone encounter this before? Please advice. Thank yo
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic