title: @Cacheable kills Hibernate Session?
body: <p>I've a problem since I switched from <code>@QueryHints</code> to <code>@Cacheable</code> on repository methods.</p>
  
  <p>I have a Controller method annotated with Spring <code>@Transactional</code>, which calls a Service method (not annotated).
  The service first gets an entity User with <code>repository.getByLogin(login)</code> and then loops on <code>parent.getChildren()</code>.</p>
  
  <p>With <code>@Cacheable</code> on <code>getByLogin(login)</code>, I have a <code>org.hibernate.LazyInitializationException</code> when trying to do <code>parent.getChildren()</code></p>
  
  <p>Controller (contains <code>@Transactional</code> which is a <strong>bad practice</strong>) :</p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  @RequestMapping(value="/users")
  public class UserController {
  
      @Autowired
      UserService userService;    
  
      @Transactional
      @RequestMapping(value="/add-son", method=POST)
      public void addSon(@RequestBody User son) {
          userService.addSon(son);
      }
  }
  </code></pre>
  
  <p>Service: </p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class UserService {
  
      @Autowired
      UserRepo repository;
  
      public void addSon(User son) {
          User parent = repository.getByLogin(user.getParent()); // call to my repo
          for(User child: parent.getChildren()) { // breaks here
              System.out.println(child)
          }
          parent.addChild(son);
      }
  }
  </code></pre>
  
  <p>My old Repository (worked): </p>
  
  <pre class="lang-java prettyprint-override"><code>public interface UserRepo extends JpaRepository&lt;User, Integer&gt; {
      @QueryHints({@QueryHint(name = COMMENT, value = "get user by login"),
                   @QueryHint(name = CACHEABLE, value = "true")})
      User findByLogin(String login);
  }
  </code></pre>
  
  <p>My new Repository (seems to kill my hibernate session): </p>
  
  <pre class="lang-java prettyprint-override"><code>public interface UserRepo extends JpaRepository&lt;User, Integer&gt; {
      @Cacheable(value = "myapp.entities.User", cacheManager = "springCacheManager", unless="#result == null")
      User findByLogin(String login);
  }
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre class="lang-java prettyprint-override"><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: myapp.entities.User.children, could not initialize proxy - no Session
          at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
          at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
          at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
          at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
          at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266)
          at myapp.services.UserService.addSon(UserService.java:19)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:497)
          at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
          at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
          at com.sun.proxy.$Proxy194.addSon(Unknown Source)
          at myapp.controllers.UserController.addSon(UserController.java:20)
          at myapp.controllers.UserController$$FastClassBySpringCGLIB$$7053b110.invoke(&lt;generated&gt;)
          at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
          at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
          at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
          at myapp.controllers.UserController$$EnhancerBySpringCGLIB$$66ec522b.addSon(&lt;generated&gt;)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  </code></pre>