title: Hibernate could not initialize proxy - no Session when accessing Object within a Thread
body: <p>How can I setup my <code>Thread</code> class to be able to access the session for which the parent class has access to?</p>
  
  <p>Currently the parent class is using <code>SomeObject</code> which has multiple <code>Set</code>'s of Objects.  These objects are required to be used by the <code>DeviceRunner</code> which is extending <code>Thread</code>. </p>
  
  <p>This application is using Spring Boot/Spring Data JPA/Hibernate.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Is it possible to <code>@Autowire</code> the <code>repository</code> as I would for a <code>@Controller</code>?  The <code>@Autowired</code> <code>repository</code> as shown below returns null.</p>
  
  <p>Setting up @Transactional has allowed me to process the SomeObject's objects but I am unable to get the <code>Repository</code> to <code>Autowire</code> so I can create/save?</p>
  
  <p>Thank you</p>
  
  <p><strong>Code DeviceRunner extending Thread</strong>:</p>
  
  <pre><code>@Transactional(propagation=Propagation.REQUIRED)
  public class DeviceRunner extends Thread {
  
      @Autowired
      public TestRunRepository repository;
  
      public SomeObject object;        
  
      private .....
  
      public DeviceRunner(args.... ) {
          // set private variables
      }
  
      public void run() {
          // do stuff
      }
  
      synchronized ....
  
  }
  </code></pre>
  
  <p><strong>Code SomeObject</strong></p>
  
  <pre><code>@Data
  @Entity
  @Table(name = "test_run")
  public class SomeObject {
  
    @ManyToMany(fetch = FetchType.LAZY)
    private Set&lt;OtherObjects&gt; otherObjects;
  
  }
  </code></pre>
  
  <p><strong>TestRunRepository</strong></p>
  
  <pre><code>@Repository
  @Transactional
  public interface TestRunRepository extends PagingAndSortingRepository&lt;TestRun, Long&gt; {
  
  }
  </code></pre>
  
  <p><strong>Rest Controller which creates the thread</strong></p>
  
  <pre><code>@Transactional(propagation=Propagation.REQUIRED)
   @RestController
  public class HomeController {
  
   @Autowired
   public TestRunRepository repository;
    ....
    @Transactional
    private void runTestRunOnDevice(TestRun testRun) {
  
  
        DeviceRunner deviceRunner = new DeviceRunner(testRun);
        deviceRunner.start();
        while (deviceRunner.isAlive());
    }
  }
  </code></pre>