配置Hibernate和Spring的Open Session in View模式和事务
At work, I took on a job to manage a new web application with Spring and Hibernate. I really have been through a tough time to learn and to work with Spring and Hibernate frameworks. I don’t have a lot of experience with them, and also no one around me has, which makes the development process even harder, especially when trying to use OSIV (Open Session in View) in Spring Web MVC. Here are the notes.
At the database level, I have many eagerly-load ManyToMany and ManyToOne (OneToMany) relationships. It made a single page loading very slow. Every time a page tried to display something, it would fetch everything related based on those relationships defined in persistent objects by annotations. The first thing came to my mind was to use lazy initialization and hoped that collections could be fetched lazily. Here came the first error when I tried to load a collection in a persistent object:
Failed to lazily initialize a collection - no session or session was closed
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
阅读全文…