title: Hibernate LazyInitializationException even in transaction
body: <p>I'm facing a very similar problem to this: <a href="https://stackoverflow.com/questions/12338512/yet-another-lazyinitializationexception-even-with-opensessioninviewfilter">Yet another LazyInitializationException even with OpenSessionInViewFilter</a></p>
  
  <p>I use Hibernate 4.2.7.Final. I have an entity which is mapped like this:</p>
  
  <pre><code>@Entity
  @Table(...)
  public class A {
      ...
      @OneToMany(fetch=FetchType.LAZY, mappedBy="b")
      private Set&lt;B&gt; bSet;
      ...
  }
  </code></pre>
  
  <p>It loads a big amount of data, that's why I need to load it when it is required. So I load a page with this contoller request mapping:</p>
  
  <pre><code>@RequestMapping("/getDetails")
  public ModelAndView showView(Model model) {
      ...
      for(B b : myService.getBSet()) {...}
      ...
  }
  </code></pre>
  
  <p>And the service is in transaction:</p>
  
  <pre><code>@Service
  @Scope(value="session")
  @Transactional("ora11transactionManager")
  public class MyServiceImpl implements MyService {
      private A a;
      ...
      public Set&lt;B&gt; getBSet() {
          return a.getBSet();
      }
  }
  </code></pre>
  
  <p>Transaction manager in the hibernate.cgf.xml:</p>
  
  <pre><code>&lt;bean id="ora11sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;
          &lt;ref bean="ora11source"/&gt;
      &lt;/property&gt;
  
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.Oracle10gDialect&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;${debug}&lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.connection.characterEncoding"&gt;UTF-8&lt;/prop&gt;
              &lt;prop key="hibernate.jdbc.use_get_generated_keys"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
      &lt;property name="packagesToScan"&gt;
          &lt;list&gt;
              &lt;value&gt;mypackage&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="ora11transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="ora11sessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>When I want to load the getDetails view, it throws the exception referencing that row in the service:</p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: &lt;my-package&gt;.A.bSet, could not initialize proxy - no Session
  </code></pre>
  
  <p>This is not the only lazy fetched collection I use, but anywhere else it works. The lazy loading must be in transaction, and it is in transaction (as you can see my service implementation)! I even added <code>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</code> in <code>web.xml</code>.</p>
  
  <p>I can't find any solution for this, please advise!</p>
  
  <p><strong>UPDATE (The exact use of my entities):</strong></p>
  
  <p>I have a large set of As, and every A has a set of B. There is a view where I can show all As, they are in a list and showed in a datatable. At the end of every rows there is a button, which calls and action. In this action I save the selected A (in myService there is a setter for selected A). This action is in controller1. And when I want to show the Bs of an A, I set which is selected and redirect to an other view. This view is managed by an other controller, that's why I save the selected A to a service (session or singleton scoped).</p>
  
  <pre><code>@Controller
  @Scope("session")
  public class Controller1 {
      ...
      public void setSelectedA(A selectedA) {
          myService.setSelectedA(selectedA);
      }
  }
  </code></pre>
  
  <p>I tried to reach the set of B even in this method, but doesn't work (the whole service is transactional, I tried to set transactional annotation only to the <code>setselectedA()</code> and <code>getBSet()</code> method, but no success).</p>