title: LazyInitializationException and @Transactional not working
body: <p>I'm using Spring 3.2.9 with multilayered architecture design broken down to 3 modules web, service, repository. In repository i defined a generic DAO class which i inherit by other entity specific DAO classes. </p>
  
  <p>The problem is that when i try to lazy fetch collection of my entity from service layer, I always get LazyInitializationException. I have tried putting @Transactional on my service class, but It appears to not work. I can only avoid exception if I initialize all lazy collections right away in the DAO class method (and only if i annotate DAO class with @Transactional), but I want to fetch those collections only when they are needed in bussiness logic, and not all in advance.</p>
  
  <p>Strange thing is that @Transactional works only in DAO layer, but not in Service layer, where it should be used. I found a couple of ways to work around this issue, but I am interested to really underestend and solve this issue, and not only make make code work.</p>
  
  <p>repository MODULE:</p>
  
  <pre><code>@Repository
  public abstract class GenericDao&lt;T&gt; {
  
      protected Class&lt;T&gt; entityClass;
  
      @PersistenceContext
      protected EntityManager entityManager;
  
      .........
  
      public T findById(long id) {
          T entity = entityManager.find(entityClass, id);
          if (entity == null) {
              throw new EntityNotFoundException(entityClass.getSimpleName(), id);
          }
  
          return entity;
      }
  }
  </code></pre>
  
  <p>My service class in service MODULE:</p>
  
  <pre><code>@Service
  public class UserServiceImpl implements UserService {
  
      private UserDao userDao;
  
      @SuppressWarnings("SpringJavaAutowiringInspection")
      @Autowired
      public UserServiceImpl(UserDao userDao) {
          this.userDao = userDao;
      }
  
      @Transactional(readOnly = true)
      @Override
      public UserDto getUserById(long id) {
          User user = userDao.findById(id);
  
          return new UserDto(user);
      }
  </code></pre>
  
  <p>The DTO constructor tries to access user.getTeams() and then exception occurs. Instead of that, collection should be fetched with additional query to DB.</p>
  
  <p>CONFIGURATION:</p>
  
  <p>repository configuration:
      ......some other configurations like datasource...</p>
  
  <pre><code>&lt;!--EntityManagerFactory--&gt;
  &lt;bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml"/&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
      &lt;/property&gt;
      &lt;property name="jpaPropertyMap"&gt;
          &lt;map&gt;
              &lt;entry key="hibernate.dialect" value="${hibernate_dialect}"/&gt;
              &lt;entry key="hibernate.hbm2ddl.auto" value="${hibernate_Hbm2ddlAuto}"/&gt;
              &lt;entry key="hibernate.show_sql" value="${hibernate_showSql}"/&gt;
              &lt;entry key="hibernate.format_sql" value="${hibernate_formatSql}"/&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;persistence-unit name="persistenceUnit"&gt;
      ...other classes..
      &lt;class&gt;com.example.entity.User&lt;/class&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>service configuration:</p>
  
  <pre><code>&lt;import resource="classpath*:META-INF/repositoryApplicationContext.xml"/&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;
  </code></pre>