title: Issue with LazyInitializationException could not initialize proxy - no Session
body: <p>I'm having this famous problem with LazyInitializationException in hibernate. I've seen a lot of question about this but still couldn't solve my problem.</p>
  
  <p>I have a many to many relation like this:</p>
  
  <p>Teen.java</p>
  
  <pre><code>public class Teen implements Serializable {
  
      @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      @JoinTable(name = "TEEN_FOLLOWER",
              joinColumns = @JoinColumn(name = "teenEmail"),
              inverseJoinColumns = @JoinColumn(name = "followerEmail"))
      private List&lt;Follower&gt; followerList;
  }
  </code></pre>
  
  <p>Follower.java</p>
  
  <pre><code>public class Follower implements Serializable {
  
      @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      @JoinTable(name = "TEEN_FOLLOWER",
              joinColumns = @JoinColumn(name = "followerEmail"),
              inverseJoinColumns = @JoinColumn(name = "teenEmail"))
      private List&lt;Teen&gt; teenList;
  }
  </code></pre>
  
  <p>One teen has <em>n</em> followers and one follower can follow <em>n</em> teens.</p>
  
  <p>I have some entries in my database already and I'm fetching all the teens from it.</p>
  
  <pre><code>List&lt;Teen&gt; teens = (List&lt;Teen&gt;) teenDao.findAll();
  
  for (Teen item : teens) {
      System.out.println("teen " + item.getEmail());
      List&lt;Follower&gt; followers = item.getFollowerList();
      for (Follower follower : followers) {
          System.out.println("follower " + follower.getEmail());
      }
  }
  </code></pre>
  
  <p>I'm getting the exception on the above code when I try to read the followers list got from getFollowerList() method.</p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.capstone.server.model.Teen.followerList, could not initialize proxy - no Session
      org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:572)
      org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
      org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:551)
      org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:140)
      org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:294)
      com.capstone.server.controller.TeenController.visualizar(TeenController.java:38)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:606)
      org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
      org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>
  
  <p>Really don't know what to do anymore. I already tried to add <code>@Transaction</code> annotation in my the method which causes the error and this  worked. However when I send the Teen object to my android application, I get the same exception while converting the object to json.</p>
  
  <p>My configuration files are:</p>
  
  <p>servlet-context.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;!-- DispatcherServlet Context: defines this servlet's request-processing 
          infrastructure --&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;annotation-driven /&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving 
          up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources 
          in the /WEB-INF/views directory --&gt;
      &lt;beans:bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;beans:property name="suffix" value=".jsp" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;beans:property name="basename" value="classpath:messages" /&gt;
          &lt;beans:property name="defaultEncoding" value="UTF-8" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="localeResolver"
          class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt;
          &lt;beans:property name="defaultLocale" value="en" /&gt;
          &lt;beans:property name="cookieName" value="myAppLocaleCookie"&gt;&lt;/beans:property&gt;
          &lt;beans:property name="cookieMaxAge" value="3600"&gt;&lt;/beans:property&gt;
      &lt;/beans:bean&gt;
  
      &lt;interceptors&gt;
          &lt;beans:bean
              class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
              &lt;beans:property name="paramName" value="locale" /&gt;
          &lt;/beans:bean&gt;
      &lt;/interceptors&gt;
  
      &lt;!-- Configure to plugin JSON as request and response in method handler --&gt;
      &lt;beans:bean
          class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"&gt;
          &lt;beans:property name="messageConverters"&gt;
              &lt;beans:list&gt;
                  &lt;beans:ref bean="jsonMessageConverter" /&gt;
              &lt;/beans:list&gt;
          &lt;/beans:property&gt;
      &lt;/beans:bean&gt;
  
      &lt;!-- Configure bean to convert JSON to POJO and vice versa --&gt;
      &lt;beans:bean id="jsonMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
      &lt;/beans:bean&gt;
  
      &lt;!-- Enable @Transactional annotation --&gt;
      &lt;tx:annotation-driven /&gt;
  
      &lt;mvc:interceptors&gt;
          &lt;beans:bean class="com.capstone.server.interceptor.LoginInterceptor" /&gt;
      &lt;/mvc:interceptors&gt;
  
      &lt;beans:bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
  
          &lt;!-- Setting maximum upload size --&gt;
          &lt;beans:property name="maxUploadSize" value="1000000" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;context:component-scan base-package="com.capstone.server" /&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>PersistenceJPAConfig.java</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySource("classpath:application.properties")
  public class PersistenceJPAConfig {
  
      @Resource
      private Environment env;
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[] {
                  Constants.PACKAGE_NAME
          });
  
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalProperties());
  
          return em;
      }
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
          // Connection data
          dataSource.setDriverClassName(env.getRequiredProperty("db.driver"));
          dataSource.setUrl(env.getRequiredProperty("db.url"));
          dataSource.setUsername(env.getRequiredProperty("db.username"));
          dataSource.setPassword(env.getRequiredProperty("db.password"));
          return dataSource;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
  
          return transactionManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
      Properties additionalProperties() {
          Properties properties = new Properties();
  
          // Hibernate properties
          properties.setProperty("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
          properties.setProperty("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
          properties.setProperty("hibernate.format_sql",
                  env.getRequiredProperty("hibernate.format_sql"));
  
          // Updates the database and generate tables, if needed
          properties.setProperty("hibernate.hbm2ddl.auto",
                  env.getRequiredProperty("hibernate.hbm2ddl.auto"));
  
          // Initializes database with admin entry in User table
          properties.setProperty("hibernate.hbm2ddl.import_files",
                  env.getRequiredProperty("hibernate.hbm2ddl.import_files"));
          properties.setProperty("hibernate.hbm2ddl.import_files_sql_extractor",
                  env.getRequiredProperty("hibernate.hbm2ddl.import_files_sql_extractor"));
  
          return properties;
      }
  }
  </code></pre>