title: Hibernate @OneToMany returns null on first call
body: <p>I am trying to populate my database, and during this, I create a associations between employees and teams. I'm trying to access the team members on a team via a hibernate OneToMany reference and doing lazy loading. This works great in my app, except when I call it right after the entities are created.</p>
  <p>I'm fairly certain this is a hibernate transaction or caching issue, as all of the rows exist in the database, but I'm struggling to understand what the issue is. I have tried converting all of the saves in the <code>populateTeamsAndEmployees</code> function to saveAndFlush, but that did not help.</p>
  <p><strong>How can I make team.getTeamMembers() work every time it is called?</strong></p>
  <p>Note: This only happens when I call the <code>seedData()</code> function via <code>RequestMapping</code>, but it doesn't happen when I call <code>seedData()</code> with a <code>@Scheduled</code> annotation</p>
  <hr />
  <h2>Function with Problem</h2>
  <pre class="lang-java prettyprint-override"><code>    void seedData() {
         populateTeamsAndEmployees()
         otherBusinessLogic()
      }
      
      public void otherBusinessLogic() {
         List&lt;Team&gt; teams = teamRepository.findAll();
      
         teams.foreach(team -&gt; {
              /*
              works great every time, returns all team members
              */
              List&lt;Employee&gt; teamMembers = employeeRepository.findEmployeeByTeamId(team.getId()); 
  
  
              /*
              this returns null when ran immediately after populateTeamsAndEmployees(), 
              but returns the same as the line above all other times
              */
              List&lt;Employee&gt; thisDoesntWork = team.getTeamMembers();
         });
      
      }
  
      public void populateTeamsAndEmployees() {
          List&lt;EmployeeFromOtherSystem&gt; employeesToConvert = otherSystemRepo.findAll();
          employeesToConvert.foreach(otherSysEmployee -&gt; {
              employeeRepository.save(otherSysEmployee.toEmployee());
          });
      }
  </code></pre>
  <h2>Entities:</h2>
  <pre><code>class Team {
      @OneToMany(mappedBy = &quot;team&quot;, fetch = FetchType.LAZY)
      private List&lt;Employee&gt; teamMembers;
  }
  
  class Employee {
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = &quot;TEAM_ID&quot;)
      private Team team;
  }
  </code></pre>