title: Failed to lazily initialize a collection of role with SpringBoot
body: <p>I have a basic SpringBoot 2.0.3.RELEASE app using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine and package as an executable JAR file with these dependencies in the pom.xml.</p>
  
  <p>I have a domain object called Company:</p>
  
  <pre><code>@Entity
  @Table(name="t_company")
  public class Company implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      public Company() {
      }
  
  
  
      /**
       * @param companyName
       */ 
      public Company(String companyName) {
          super();
          this.name = companyName;
      }
  
  
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @NotEmpty
      @Length(max = 100)
      private String name;
  
  
      @OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private Set&lt;User&gt; users = new HashSet&lt;&gt;();
  
  ..
  }
  </code></pre>
  
  <p>Repository Layer:</p>
  
  <pre><code>public interface CompanyRepository extends CrudRepository&lt;Company, Long&gt; {
  
      @Query("select co from Company co join co.users us where co = ?1")
      Company companyUsers (Company company);
  
  }
  </code></pre>
  
  <p>Service Layer:</p>
  
  <pre><code>@Service
  @Transactional(readOnly = true)
  public class CompanyService {
  
       @Autowired
       private CompanyRepository companyRepository;
  
       public Company companyUsers (Company company) {
          return companyRepository.companyUsers(company);
       }
  
  }
  </code></pre>
  
  <p>Junit file:</p>
  
  <pre><code>@Test
      public void testCompanyUsers() throws Exception {
  
          Iterable&lt;Company&gt; companies = companyService.findAll();
          Company company = companies.iterator().next();
  
          assertNotNull (company);
  
          company = companyService.companyUsers(company);
          assertTrue (((Collection&lt;?&gt;) company.getUsers()).size() &gt; 0);           
      }
  </code></pre>
  
  <p>But when I run the test I get this error:</p>
  
  <blockquote>
    <p>failed to lazily initialize a collection of role:
    com.cor.backend.persistence.domain.backend.Company.users, could not
    initialize proxy - no Session</p>
  </blockquote>