0

I am in a service, with a scheduled task, and I want to get an object from the database. It has EAGER associations, so the find method should get it totally.

@Service
public class CustomTask {

    @Autowired
    CustomRepository customRepository;

    @Scheduled(fixedRate = 1000)
    public void action() {
         customRepository.find(1);
    }
}

But here it doesn't work. The associations are null.

While inside a Spring Boot Controller, the repository method works perfectly.

Do you know I can get my whole object in this Scheduled method of a Service?

johanvs
  • 4,273
  • 3
  • 24
  • 26

1 Answers1

0

The scheduled task is called at the beginning of the app, at a moment where the environment may not be totally initialized.

With an initial delay to the task, I can access my whole object :

@Scheduled(initialDelay=10000, fixedRate = 1000)

NB : It is more a workaround than a fix.

johanvs
  • 4,273
  • 3
  • 24
  • 26