title: Lazy Loading of One to Many Doesnt Work
body: <p>I have 2 entities - Media and Keyword with one to many relationship. I though I marked the fetch to Lazy Madia is still fetching all the keywords. </p>
  
  <p>I am using spring 3.2 Hibernate 4.3.4</p>
  
  <p><strong>Media Entity:</strong> </p>
  
  <pre><code>@Entity
  public class Media {
  @Id
      @NotNull
      @Column(name = "mediaid")
      private String mediaId;
  
      @NotNull
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "media", fetch = FetchType.LAZY)
      private List&lt;Keyword&gt; keywords;
  
      public List&lt;Keyword&gt; getKeywords() {
          return keywords;
      }
  
      public void setKeywords(List&lt;Keyword&gt; keywords) {
          if ( this.keywords!=null &amp;&amp; this.keywords.size()&gt;0 )
              this.keywords.addAll(keywords);
          else
              this.keywords = keywords;
  
          for (Keyword keyword : keywords) {
              keyword.setMedia(this);
          }
      }
  }
  </code></pre>
  
  <p><strong>Keyword Entity:</strong></p>
  
  <pre><code>@Entity(name = "keywords")
  public class Keyword {
  
      @Id
      @NotNull
      @Column(unique = true)
      private String idKey;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "idmedia")
      private Media media;
  
      public Media getMedia() {
          return media;
      }
  
      public void setMedia(Media media) {
          this.media = media;
  
          if (idKey == null || idKey.isEmpty())
              setIdKey(this.media.getMediaId());
          else
              setIdKey(this.media.getMediaId() + "_" + idKey);
  
          if (!media.containsKeyword(this))
              media.getKeywords().add(this);
      }
  }
  </code></pre>
  
  <p>I can see the entities being fetch in the show_sql output but also this test fails</p>
  
  <pre><code>@WebAppConfiguration
  @ContextConfiguration(classes = {PersistenceConfig.class})
  @Transactional
  @TransactionConfiguration(defaultRollback = true)
  public class MediasRepositoryTest extends AbstractTransactionalTestNGSpringContextTests {
  
      @Autowired
      MediasRepository mediasRepository;
  
      @PersistenceContext
      EntityManager manager;
  
      public void thatMediaLazyLoadsKeywords() throws Exception {
          PersistenceUnitUtil unitUtil = manager.getEntityManagerFactory().getPersistenceUnitUtil();
  
          Media media = DomainFixtures.createMedia();
          Media savedMedia = mediasRepository.save(media);
          Media retrievedMedia = mediasRepository.findOne(savedMedia.getMediaId());
  
          assertNotNull(retrievedMedia);
          assertFalse(unitUtil.isLoaded(retrievedMedia, "keywords"));
      }
  }
  </code></pre>
  
  <p><strong>Configuration:</strong></p>
  
  <pre><code>@EnableTransactionManagement
  public class PersistenceConfig {
      static Logger logger = LoggerFactory.getLogger(PersistenceConfig.class);
      @Autowired
      private Environment env;
  
  //    @Value("${init-db:false}")
      private String initDatabase = "false";
  
      @Bean
      public DataSource dataSource()
      {
          logger.info("Starting dataSource");
          BasicDataSource dataSource = new BasicDataSource();
          logger.info("jdbc.driverClassName"+ env.getProperty("jdbc.driverClassName"));
          dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
          dataSource.setUrl(env.getProperty("jdbc.url"));
          dataSource.setUsername(env.getProperty("jdbc.username"));
          dataSource.setPassword(env.getProperty("jdbc.password"));
          logger.info("End dataSource");
          return dataSource;
      }
  
      @Bean
      public EntityManagerFactory entityManagerFactory() throws SQLException
      {
          logger.info("Starting entityManagerFactory");
  
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          vendorAdapter.setGenerateDdl(Boolean.TRUE);
  
          vendorAdapter.setShowSql(Boolean.parseBoolean(env.getProperty("hibernate.show_sql")));
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  
          factory.setJpaVendorAdapter(vendorAdapter);
          factory.setPackagesToScan("....");
          factory.setDataSource(dataSource());
  
          Properties jpaProperties = new Properties();
          jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
          jpaProperties.put("hibernate.enable_lazy_load_no_trans", true);
          jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
          factory.setJpaProperties(jpaProperties);
          factory.afterPropertiesSet();
          logger.info("End entityManagerFactory");
          return factory.getObject();
      }
  
      .
      .
      .
  }
  </code></pre>