title: Java Lazy Loading with Hibernate/JPA/Spring
body: <p>I'm having an issue with converting an application to separate REST Server + Web Application rather than having a single application that handles pages + business logic.</p>
  
  <p>I'm having a problem with an org.hibernate.LazyInitializationException being thrown when Jackson is serialising the output. </p>
  
  <p>Below is my JPA configuration and the class in question. I'm aware the lazy initialisation is being caused by the List member, but I don't want this to be populated in the JSON returned. Hence the @JsonIgnore annotation.</p>
  
  <p><strong>PersistenceConfiguration.java</strong></p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackageClasses = {ForumRepository.class})
  public class PersistenceConfiguration {
  
  @Bean
  public EntityManagerFactory entityManagerFactory() {
      HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  
      factory.setJpaVendorAdapter(vendorAdapter);
      factory.setPackagesToScan("com.mypackage.forum.api.models");
      factory.setDataSource(secureDataSource());
      factory.afterPropertiesSet();
  
      return factory.getObject();
  }
  
  @Bean
  public JpaTransactionManager transactionManager() {
      EntityManagerFactory factory = entityManagerFactory();
      return new JpaTransactionManager(factory);
  }
  
  @Bean
  public HibernateExceptionTranslator exceptionTranslator() {
      return new HibernateExceptionTranslator();
  }
  
  @Bean
  public DataSource secureDataSource() {
      try {
          Context ctx = new InitialContext();
          return (DataSource) ctx.lookup("java:jboss/datasources/ForumDS");
      } catch (Exception e) {
          return null;
      }
  }
  }
  </code></pre>
  
  <p><strong>ForumGroup.java</strong></p>
  
  <pre><code>@Entity
  public class ForumGroup {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;
  
  @JsonIgnore
  @OneToMany(mappedBy = "group")
  private List&lt;Forum&gt; forums;
  
  private String name;
  private String description;
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public List&lt;Forum&gt; getForums() {
      return forums;
  }
  
  public void setForums(List&lt;Forum&gt; forums) {
      this.forums = forums;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getDescription() {
      return description;
  }
  
  public void setDescription(String description) {
      this.description = description;
  }
  
  @Override
  public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
  
      ForumGroup that = (ForumGroup) o;
  
      if (id != null ? !id.equals(that.id) : that.id != null) return false;
      if (name != null ? !name.equals(that.name) : that.name != null) return false;
  
      return true;
  }
  
  @Override
  public int hashCode() {
      int result = id != null ? id.hashCode() : 0;
      result = 31 * result + (name != null ? name.hashCode() : 0);
      return result;
  }
  
  @Override
  public String toString() {
      return "ForumGroup{" +
              "id=" + id +
              ", name='" + name + '\'' +
              ", description='" + description + '\'' +
              '}';
  }
  }
  </code></pre>
  
  <p>I presume that I need to configure some sort of Session Manager but not sure the best way to go about doing it in JavaConfig, I've tried various methods via Google with no joy.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I've now managed to resolve this by 1) Adding the @Fetch() annotation to the relevant collections, which means that the collections get populated 2) I have stopped Entities/Models being returned as JSON objects and instead use separate DTOs</p>