title: LazyInitializationException: could not initialize proxy - no Session in Spring and Hibernate
body: <p>I'm getting this error: "org.hibernate.LazyInitializationException: could not initialize proxy - no Session" in my webapp. I use Spring and Hibernate, and I execute the queries with "sessionFactory.getCurrentSession()".</p>
  
  <p>I read a lot about this problem, so I tried with the answers that I found, but it doesn't work.
  I'm currently using "OpenSessionInViewFilter" and "OpenSessionInViewInterceptor".</p>
  
  <p>I attach my configuration files.</p>
  
  <p>Thank you in advance.</p>
  
  <p>Stack trace</p>
  
  <pre><code>org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
  at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
  at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
  at involve.gbi.persistence.entities.Actividad_$$_javassist_33.getDireccion(Actividad_$$_javassist_33.java)
  at involve.gbi.cro.business.data.model.HibernateCRODCM.addActividadesToWc(HibernateCRODCM.java:676)
  at involve.gbi.cro.business.data.model.HibernateCRODCM.generateWorkingCalendars(HibernateCRODCM.java:370)
  at involve.gbi.cro.business.algorithm.CROVisitsAlgorithmPeriodByPeriodDayByDay.setIntervaloOptimizacionActivo(CROVisitsAlgorithmPeriodByPeriodDayByDay.java:126)
  at involve.gbi.cro.business.algorithm.CROVisitsAlgorithmPeriodByPeriodDayByDay.run(CROVisitsAlgorithmPeriodByPeriodDayByDay.java:738)
  </code></pre>
  
  <p>applicationContext.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:p="http://www.springframework.org/schema/p"
      xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util-3.2.xsd
          http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task-3.2.xsd "&gt;
  
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="example.gbi" /&gt;
      &lt;!-- &lt;context:property-placeholder location="classpath:appConfig.properties,classpath:jdbc.properties" 
          system-properties-mode="OVERRIDE" ignore-unresolvable="true" /&gt; --&gt;
      &lt;!-- &lt;import resource="spring-ws-config.xml"/&gt; --&gt;
      &lt;import resource="database-config.xml" /&gt;
      &lt;import resource="application-security.xml"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>application-security.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/security"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security.xsd"&gt;
      &lt;global-method-security secured-annotations="enabled" pre-post-annotations="enabled" /&gt;
      &lt;http auto-config="false"  use-expressions="true" disable-url-rewriting="true" create-session="ifRequired"&gt;
  
             &lt;intercept-url pattern="/admin**" access="hasRole('Administrator')" /&gt; 
             &lt;intercept-url pattern="/admin/j_spring_security_check" access="permitAll"/&gt;
            &lt;intercept-url pattern="/admin/login.jsp" access="permitAll" /&gt;
            &lt;intercept-url pattern="/admin/logout.jsp" access="permitAll" /&gt;
            &lt;intercept-url pattern="/admin/accessdenied.jsp" access="permitAll" /&gt;
  
          &lt;form-login login-page="/admin/login.jsp" login-processing-url="/admin/j_spring_security_check" default-target-url="/admin/api/welcome" authentication-failure-url="/admin/accessdenied.jsp" /&gt;
          &lt;logout logout-success-url="/admin/logout.jsp" /&gt;
      &lt;/http&gt;
      &lt;authentication-manager  erase-credentials="false"&gt;
           &lt;authentication-provider user-service-ref="myUserDetailsService" /&gt;
      &lt;/authentication-manager&gt;
  
      &lt;beans:bean name="myUserDetailsService" class="security.UserDetailService" /&gt;
  
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>database-config.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:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx" 
         xmlns:aop="http://www.springframework.org/schema/aop"
         xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"&gt;
  
  &lt;!--    &lt;context:property-placeholder location="classpath:persistence-mysql.properties" /&gt; --&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
  
          &lt;property name="configLocation"&gt;
              &lt;value&gt;
                  classpath:hibernate.cfg.xml
              &lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  
      &lt;!-- The transaction advice - setting attributes for transactions --&gt;
      &lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;
          &lt;tx:attributes&gt;
              &lt;tx:method name="get*" read-only="true" /&gt;
              &lt;tx:method name="find*" read-only="true" /&gt;
              &lt;tx:method name="add*" propagation="REQUIRED" /&gt;
              &lt;tx:method name="update*" propagation="REQUIRED" isolation="SERIALIZABLE" /&gt;
              &lt;tx:method name="remove*" propagation="REQUIRED" isolation="SERIALIZABLE" /&gt;
              &lt;tx:method name="*" /&gt;
          &lt;/tx:attributes&gt;
      &lt;/tx:advice&gt;
      &lt;!-- Establish the AOP transaction cross cutting concern and define which classes/methods are transactional  --&gt;
      &lt;aop:config&gt;
          &lt;aop:pointcut id="serviceOperations" expression="execution(* persistence.manager.*.*(..))" /&gt;
          &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperations" /&gt;
      &lt;/aop:config&gt;
  
  
  
      &lt;bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>hibernate.cfg.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" &gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.connection.datasource"&gt;java:comp/env/jdbc/CRO&lt;/property&gt;
  
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
          &lt;property name="hibernate.jdbc.batch_size"&gt;0&lt;/property&gt;
          &lt;property name="hibernate.c3p0.min_size"&gt;1&lt;/property&gt;
          &lt;property name="hibernate.c3p0.max_size"&gt;300&lt;/property&gt;
          &lt;property name="hibernate.c3p0.acquireIncrement"&gt;5&lt;/property&gt;
          &lt;property name="hibernate.c3p0.idle_test_period"&gt;10&lt;/property&gt;
          &lt;property name="hibernate.c3p0.timeout"&gt;600&lt;/property&gt;
          &lt;property name="hibernate.c3p0.validate"&gt;true&lt;/property&gt;            
          &lt;property name="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory&lt;/property&gt;
          &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.cache.use_query_cache"&gt;true&lt;/property&gt;
          &lt;property name="hibernate.generate_statistics"&gt;true&lt;/property&gt;
  
          &lt;property name="configLocation"&gt;hibernate.cfg.xml"&lt;/property&gt;
          &lt;mapping class="persistence.entities.LocationTrack"/&gt;
          ··· A lot of mappings class ···
          &lt;mapping class="persistence.entities.VisitaHistorico"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>mvc-config.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:p="http://www.springframework.org/schema/p"
         xmlns:task="http://www.springframework.org/schema/task"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:util="http://www.springframework.org/schema/util"
         xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  
  
         xsi:schemaLocation="http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util-3.2.xsd
          http://www.springframework.org/schema/task
          http://www.springframework.org/schema/task/spring-task-3.2.xsd
          "&gt;
  
  
  
      &lt;context:component-scan base-package="example.*" /&gt;
  
        &lt;mvc:annotation-driven&gt;
            &lt;mvc:message-converters register-defaults="true"&gt;
                &lt;bean class="web.configuration.GsonHttpMessageConverter" &gt;
                &lt;property name="supportedMediaTypes" value = "application/json;charset=UTF-8" /&gt;
                &lt;/bean&gt;
            &lt;/mvc:message-converters&gt;
        &lt;/mvc:annotation-driven&gt;
  
      &lt;mvc:interceptors&gt;
              &lt;bean class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor"&gt;
                  &lt;property name="sessionFactory"&gt;
                      &lt;ref local="sessionFactory"/&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
          &lt;/mvc:interceptors&gt;
  
      &lt;import resource="applicationContext.xml"/&gt;
  
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources 
          in the /WEB-INF/views directory --&gt;
      &lt;bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
      &lt;task:annotation-driven /&gt;
      &lt;mvc:resources mapping="/resources/**" location="/www/" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <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"
      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;CROQueryServletTesting&lt;/display-name&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
          &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
          &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
          &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
          &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;OpenSessionInViewFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;OpenSessionInViewFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&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;/admin/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;SERVLET-REST&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;classpath:mvc-config.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
          &lt;async-supported&gt;true&lt;/async-supported&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;SERVLET-REST&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/admin/api/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;SERVLET-REST&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/api/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;SERVLET-REST&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/CROBasicQueryServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
    &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
    &lt;/listener&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
  
      &lt;servlet&gt;
          &lt;description&gt;&lt;/description&gt;
          &lt;display-name&gt;CROLocationTrackingServlet&lt;/display-name&gt;
          &lt;servlet-name&gt;CROLocationTrackingServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;web.CROLocationTrackingServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;CROLocationTrackingServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/CROLocationTrackingServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
  &lt;/web-app&gt;
  </code></pre>