My Delete() and DeleteById() methods in JPARepository don't work, I have no idea why. I have 3 classes: Station, Line and StationLine. StationLine is just a class which binds first two and has some additional atributes which I use later in code.
Line has attribute:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "line", cascade = CascadeType.ALL)
private List<StationLine> stations;
Station has attribute:
@OneToMany(fetch = FetchType.LAZY, mappedBy="station", cascade=CascadeType.ALL)
private List<StationLine> stationLines;
StationLine has attributes:
@ManyToOne(optional=false)
private Station station;
@ManyToOne(optional=false)
private Line line;
The problem is in next part of code:
for(StationLine sl : stationsToRemove) {
StationLine found = stationLineRepository.findById(sl.getId()).orElse(null);
if(found == null) { throw new StationNotFoundException("Station " + sl.getStation().getName() + " not found!"); }
stationLineRepository.deleteById(found.getId());
found = stationLineRepository.findById(found.getId()).orElse(null);
if(found != null) {throw new StationNotFoundException("Station " + found.getId() + " " + found.getStation().getName() + " should be null but it is not!");}
}
As you can see, I have added some code before and after DeleteById() which is not necessary but just to check if all works fine. Before deletion it always finds StationLine object which is ok, but after it also finds it which is not good if it was deleted prior.
Same situation for Delete().
If I'm correct, with these relations, Station and Line should be completely independent from StationLine, so it's deletion should not affect on them. I have also tried to delete it directly in database and it works without any errors, so that should mean that relations are ok, right?
Please help!