title: Lazy initialize in Spring MVC
body: <p>Despite using configuration below I got lazy initialize error:</p>
  
  <blockquote>
    <p>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: beans.Restaurant.tags, no session or session was closed</p>
  </blockquote>
  
  <p>and here the </p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5"&gt;
  &lt;display-name&gt;RestFinderWebApp&lt;/display-name&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;!-- &lt;welcome-file&gt;index.html&lt;/welcome-file&gt; --&gt;
  &lt;/welcome-file-list&gt;
  &lt;!-- Spring security: --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;
          /WEB-INF/applicationContext-security.xml,
          /WEB-INF/spring-servlet.xml
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;encoding&lt;/param-name&gt;
          &lt;param-value&gt;UTF-8&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;SpringOpenEntityManagerInViewFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;SpringOpenEntityManagerInViewFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p>spring-servlet.xml:</p>
  
  <pre><code>    &lt;?xml  version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
      &lt;bean id="txManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory"&gt;
              &lt;ref local="sessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
      &lt;bean
          class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;bean class="org.springframework.jmx.export.MBeanExporter"&gt;
          &lt;property name="autodetect" value="false" /&gt;
          &lt;property name="assembler"&gt;
              &lt;bean id="jmxAssembler"
                  class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler"&gt;
                  &lt;property name="attributeSource"&gt;
                      &lt;bean
                          class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" /&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
  
      &lt;bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource"
          p:basename="messages" /&gt;
  
      &lt;bean id="openSessionInViewInterceptor"
          class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="myPersistence" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaDialect"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /&gt;
          &lt;/property&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="showSql" value="false" /&gt;
                  &lt;property name="generateDdl" value="true" /&gt; 
                  &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          (...)
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  (...)
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.useUnicode"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.characterEncoding"&gt;UTF-8&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven /&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
      &lt;bean id="myUserDAO" class="dao.UserDAO"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
      &lt;bean id="myRestaurantDAO" class="dao.RestaurantDAO"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean name="/mojekonto.htm" class="web.HomeUserController"&gt;
          &lt;property name="userDAO" ref="myUserDAO" /&gt;
          &lt;property name="restaurantDAO" ref="myRestaurantDAO" /&gt;
          &lt;property name="loggedUser" ref="LoggedUser" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>my controller:</p>
  
  <pre><code>public class HomeUserController extends MultiActionController {
  private UserDAO userDAO;
  private TagDAO tagDAO;
  
  public void setTagDAO(TagDAO tagDAO) {
      this.tagDAO = tagDAO;
  }
  
  public void setUserDAO(UserDAO userDAO) {
      this.userDAO = userDAO;
  }
  
  private LoggedUser loggedUser;
  
  public void setLoggedUser(LoggedUser loggedUser) {
      this.loggedUser = loggedUser;
  }
  
  private RestaurantDAO restaurantDAO;
  
  
  public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
      this.restaurantDAO = restaurantDAO;
  }
  
  private FollowDAO followDAO;
  
  public void setFollowDAO(FollowDAO followDAO) {
      this.followDAO = followDAO;
  }
  private CommentDAO commentDAO;
  
  
  public void setCommentDAO(CommentDAO commentDAO) {
      this.commentDAO = commentDAO;
  }
  @InitBinder
  protected void initBinder(HttpServletRequest request,
          ServletRequestDataBinder binder) throws Exception {
  
      super.initBinder(request, binder);
  
      binder.registerCustomEditor(List.class, "tags",new CustomCollectionEditor(List.class){
  
          @Override
          protected Object convertElement(Object element) {
              Tag tag = new Tag();
  
              if (element != null) {
                  int id = Integer.valueOf(element.toString());
                  tag.setId(id);
              }
              return tag;
          }
      });
  
  }
  public ModelAndView mojekonto(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
  
      User user = loggedUser.getLoggedUser();
      ModelMap modelMap = new ModelMap();
      Restaurant restaurant = new Restaurant();
      Restaurant userRestaurant = new Restaurant();
      userRestaurant = restaurantDAO.findRestaurantByUser(user.getUsername());
  
      modelMap.addAttribute("user", user);
      modelMap.addAttribute("restaurant", restaurant);
      modelMap.addAttribute("userRestaurant", userRestaurant);
  
      return new ModelAndView("mojekonto", modelMap);
  }
  </code></pre>
  
  <p>My class - restaurant:</p>
  
  <pre><code>    @Entity
  @Table(name="restaurants")
  public class Restaurant {
      @Id
      @GeneratedValue
      private int id;
      private String name;
      private String street;
      private String city;
      private String country;
      private String postal_code;
      private String telephone;
      private String logo;
      private String image;
      private String description;
      private float longitude;
      private float latitude;
      private Date last_update;
      @ManyToMany //it works fine...
      @JoinTable(name="user_restaurant_owner",
              joinColumns={@JoinColumn(name="restaurant_id")},
              inverseJoinColumns={@JoinColumn(name="username")})
      private List&lt;User&gt; owner;
      @ManyToMany //but that doesn't...
      @JoinTable(name="restaurant_tag",
              joinColumns={@JoinColumn(name="restaurant_id")},
              inverseJoinColumns={@JoinColumn(name="tag_id")})
      private List&lt;Tag&gt; tags;
          //getters and setters:
  </code></pre>
  
  <p>my view jsp:</p>
  
  <pre><code> &lt;c:forEach items="${userRestaurant.tags}" var="current"&gt;
  do something
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>I got error when it is in "{userRestaurant.tags}"</p>