How can I setup my Thread class to be able to access the session for which the parent class has access to?
Currently the parent class is using SomeObject which has multiple Set's of Objects. These objects are required to be used by the DeviceRunner which is extending Thread.
This application is using Spring Boot/Spring Data JPA/Hibernate.
Update
Is it possible to @Autowire the repository as I would for a @Controller? The @Autowired repository as shown below returns null.
Setting up @Transactional has allowed me to process the SomeObject's objects but I am unable to get the Repository to Autowire so I can create/save?
Thank you
Code DeviceRunner extending Thread:
@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 SomeObject
@Data
@Entity
@Table(name = "test_run")
public class SomeObject {
@ManyToMany(fetch = FetchType.LAZY)
private Set<OtherObjects> otherObjects;
}
TestRunRepository
@Repository
@Transactional
public interface TestRunRepository extends PagingAndSortingRepository<TestRun, Long> {
}
Rest Controller which creates the thread
@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());
}
}