vlad wrote:
All three findings are very important and I think your issue is caused by 2). Nevertheless, you should fix all of them because 3) is related to 2) when it comes to removing child entities, and 1) is just a best practice.
I had a go.
EPISODE
Code:
public Episode() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// Other fields
@OneToMany(mappedBy = "episode", cascade=CascadeType.ALL)
private List<Stat> stats = new ArrayList<>();
// GETTERS & SETTERS
public void addStat(Stat stat) {
stats.add(stat);
stat.setEpisode(this);
}
public void removeStat(Stat stat) {
stats.remove(stat);
stat.setEpisode(null);
}
}
STAT
Code:
@Entity
public class Stat {
public Stat() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
private Offences code;
private Integer result;
private Boolean physicalInjury;
@ManyToOne
@JoinColumn(name="episode_id")
private Episode episode;
// GETTERS SETTERS
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Stat )) return false;
return id != null && id.equals(((Stat) o).id);
}
@Override
public int hashCode() {
return 31;
}
}
Hopefully I am on the right track up until this point?
Then in the EpisodeService saveEpisode method I have:
Code:
public Episode saveEpisode(Episode episode) {
// OTHER STUFF
for (Stat stat : episode.getStats()) {
if (stat!=null) {
//stat.setEpisode(episode);
episode.addStat(stat);
}
}
return episodeRepository.save(episode);
}
But it did not like the for loop. Error was :
"java.util.ConcurrentModificationException: null
So I googled and tried a iterator, but the types were wrong for the episode.addStat(stat).
Suggestions?
Thank you again