title: Join fetch in JPA sometimes causes LazyInitializationException
body: <p>I am having this issue with JPA in my application. I have a parent entity Artist with one-to-one/many etch relations to other entities which I have all set to be lazily fetched. I fetch these entities using join queries. This all seems to work fine but sometimes i get a LazyInitializationException. I am using Stateless EJB's with JPA in the backend and Spring MVC in the web layer. Here is the method:</p>
  
  <pre><code>public Artist getArtistWithChildren(long id, boolean isFetchReviews, boolean isFetchRequests, boolean isFetchGigs, boolean isFetchVenues) {
      StringBuilder sql = new StringBuilder();
      sql.append("select a from Artist a join fetch a.members mrs");
      if(isFetchReviews) {
          sql.append(" left join a.reviews rvs");
      } if(isFetchRequests) {
          sql.append(" left join a.requests rqs");
      } if(isFetchGigs) {
          sql.append(" left join a.gigs gs");
      } if(isFetchVenues) {
          sql.append(" left join a.venues vs");
      }
  
      sql.append(" where a.id=:id");
  
      TypedQuery&lt;Artist&gt; query = em.createQuery(sql.toString(), Artist.class);
      query.setParameter("id", id);
      query.setMaxResults(1);
      List&lt;Artist&gt; resultList = query.getResultList();
      return resultList.get(0);
  }
  </code></pre>
  
  <p>And here is the entity class Artist</p>
  
  <pre><code>@Entity
  @Table(name="ARTIST")
  public class Artist extends DescribingEntity {
  
  private static final long serialVersionUID = -7264327449601568983L;
  
  @ManyToMany(mappedBy="artists", targetEntity=Member.class, fetch=FetchType.LAZY, cascade={MERGE, REFRESH})
  private List&lt;Member&gt; members;
  
  @OneToMany(mappedBy="artist", targetEntity=VenueReview.class, fetch=FetchType.LAZY, cascade={MERGE, REFRESH, REMOVE})
  private List&lt;VenueReview&gt; reviews;
  
  @OneToMany(mappedBy="artist", targetEntity=Gig.class, fetch=FetchType.LAZY, cascade={MERGE, REFRESH, REMOVE})
  private List&lt;Gig&gt; gigs;
  
  @OneToMany(mappedBy="artist", targetEntity=GigRequest.class, fetch=FetchType.LAZY, cascade={MERGE, REFRESH, REMOVE})
  private List&lt;GigRequest&gt; requests;
  
  @ManyToMany(cascade={MERGE, REFRESH}, fetch=FetchType.LAZY)
  @JoinTable(name="VENUE_ARTIST_REL", 
      joinColumns=@JoinColumn(name="ARTIST_ID", referencedColumnName="ARTIST_ID"), 
      inverseJoinColumns=@JoinColumn(name="VENUE_ID", referencedColumnName="VENUE_ID"))
  private List&lt;Venue&gt; venues;
  
  getters and setters...
  </code></pre>
  
  <p>I then go into debug mode to find out what went wrong and as I am stepping through the method it returns no error and no exception is thrown. Could it be that the collection is returned too soon so that all data from the DB has not had the time to populate correctly? I am a newbie when it comes to JPA so please let me know what I did wrong. Here is an example. I am using left joins here since I dont HAVE to get the results, but I doo need an instantiated collection.</p>
  
  <p>I have tested the same thing using Arquillian and no errors, I also have similar methods for other entities where the scenario is the same, simply running it causes the error while step-through debugging don't.</p>
  
  <p>Using <code>Hibernate.initialize(Object o)</code> on the child collections works fine but to my knowledge it is not a good thing to do since I then have to make a DB query for each child (correct me if I'm wrong).</p>
  
  <p>I added a pastebin link to the stack trace</p>
  
  <p><a href="http://pastebin.com/tLgHQzS8" rel="nofollow noreferrer">Stack Trace</a></p>