I'm strugglingtrying to "refresh" an entity (Foo) that look like this:
public class Foo {
//...
@OneToMany(mappedBy = "fooRef", fetch = FetchType.LAZY)
private List<Bar> bars;
}
public class Bar {
//...
@Column(name = "FOO_ID")
private Long fooRef;
@Column(name = "STATE")
@Enumerated(EnumType.STRING)
private State state;
}
As Spring JPA doesn't implement the refresh method, I just query the entity again. This is the code that it doesn't work and I dont know why:
for(int i=0; i<MAX_RETRIES; i++) {
List<Bar> bars = foo.getBars();
// check bars state
if(someBarIsBad) {
try {
TimeUnit.SECONDS.sleep(delay);
} catch (InterruptedException ignored) {
}
} else {
break;
}
foo = fooRepo.findById(fooId); // "refresh"
}
The goal is to continue retry until all bars have the desired state.
The test I'm doing is placing a breakpoint at the last instruction of the loop, so when it break, I go the the table and change the state of all of the bars to the desired one to then exit the loop. The thing is that the new returned foo
, the bars
doesn't reflect the change I made.
I tried to remove the sleep, but that is not the problem. I think it must be something with the mapping.