title: How to resolve LazyInitializationException in Spring Data JPA?
body: <p>I have two classes that have a one-to-many relation. When I try to access the lazily loaded collection I get the <code>LazyInitializationException</code>.
  I have been searching the web for a while and now I know that I get the exception because the session that was used to load the class which holds the collection is closed.
  However, I did not find a solution (or at least I did not understand them). Basically I have these classes:</p>
  <p><strong>User</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;user&quot;)
  public class User {
  
      @Id
      @GeneratedValue
      @Column(name = &quot;id&quot;)
      private long id;
  
      @OneToMany(mappedBy = &quot;creator&quot;)
      private Set&lt;Job&gt; createdJobs = new HashSet&lt;&gt;();
  
      public long getId() {
          return id;
      }
  
      public void setId(final long id) {
          this.id = id;
      }
  
      public Set&lt;Job&gt; getCreatedJobs() {
          return createdJobs;
      }
  
      public void setCreatedJobs(final Set&lt;Job&gt; createdJobs) {
          this.createdJobs = createdJobs;
      }
  
  }
  </code></pre>
  <p><strong>UserRepository</strong></p>
  <pre><code>public interface UserRepository extends JpaRepository&lt;User, Long&gt; {}
  </code></pre>
  <p><strong>UserService</strong></p>
  <pre><code>@Service
  @Transactional
  public class UserService {
  
      @Autowired
      private UserRepository repository;
  
      boolean usersAvailable = false;
  
      public void addSomeUsers() {
          for (int i = 1; i &lt; 101; i++) {
              final User user = new User();
  
              repository.save(user);
          }
  
          usersAvailable = true;
      }
  
      public User getRandomUser() {
          final Random rand = new Random();
  
          if (!usersAvailable) {
              addSomeUsers();
          }
  
          return repository.findOne(rand.nextInt(100) + 1L);
      }
  
      public List&lt;User&gt; getAllUsers() {
          return repository.findAll();
      }
  
  }
  </code></pre>
  <p><strong>Job</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;job&quot;)
  @Inheritance
  @DiscriminatorColumn(name = &quot;job_type&quot;, discriminatorType = DiscriminatorType.STRING)
  public abstract class Job {
  
      @Id
      @GeneratedValue
      @Column(name = &quot;id&quot;)
      private long id;
  
      @ManyToOne
      @JoinColumn(name = &quot;user_id&quot;, nullable = false)
      private User creator;
  
      public long getId() {
          return id;
      }
  
      public void setId(final long id) {
          this.id = id;
      }
  
      public User getCreator() {
          return creator;
      }
  
      public void setCreator(final User creator) {
          this.creator = creator;
      }
  
  }
  </code></pre>
  <p><strong>JobRepository</strong></p>
  <pre><code>public interface JobRepository extends JpaRepository&lt;Job, Long&gt; {}
  </code></pre>
  <p><strong>JobService</strong></p>
  <pre><code>@Service
  @Transactional
  public class JobService {
  
      @Autowired
      private JobRepository repository;
  
      public void addJob(final Job job) {
          repository.save(job);
      }
  
      public List&lt;Job&gt; getJobs() {
          return repository.findAll();
      }
  
      public void addJobsForUsers(final List&lt;User&gt; users) {
          final Random rand = new Random();
  
          for (final User user : users) {
              for (int i = 0; i &lt; 20; i++) {
                  switch (rand.nextInt(2)) {
                  case 0:
                      addJob(new HelloWorldJob(user));
                      break;
                  default:
                      addJob(new GoodbyeWorldJob(user));
                      break;
                  }
              }
          }
      }
  
  }
  </code></pre>
  <p><strong>App</strong></p>
  <pre><code>@Configuration
  @EnableAutoConfiguration
  @ComponentScan
  public class App {
  
      public static void main(final String[] args) {
          final ConfigurableApplicationContext context = SpringApplication.run(App.class);
          final UserService userService = context.getBean(UserService.class);
          final JobService jobService = context.getBean(JobService.class);
  
          userService.addSomeUsers();                                 // Generates some users and stores them in the db
          jobService.addJobsForUsers(userService.getAllUsers());      // Generates some jobs for the users
  
          final User random = userService.getRandomUser();            // Picks a random user
  
          System.out.println(random.getCreatedJobs());
      }
  
  }
  </code></pre>
  <p>I have often read that the session has to be bound to the current thread, but I don't know how to do this with Spring's annotation based configurations.
  Can someone point me out how to do that?</p>
  <p>P.S. I want to use lazy loading, thus eager loading is no option.</p>