I have one-one relationship between Team and Player, I can create a team with the following JSON
{ "id": 1, "name": "MyTeam5", "players": [ { "name": "player5" }, { "name":"player10" } ] }
However, when I try to remove the child by removing it from the list with the following JSON
{ "id": 1, "name": "MyTeam5", "players": [ { "id": 2 "name": "player5" } ] }
I get an error
"A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: org.acdomain.user.Team.players"
Here is the code for my team and player -
@Data @Entity public class Team { @Id @GeneratedValue private Integer id; private String name; @JsonManagedReference @OneToMany(mappedBy="team", cascade= CascadeType.ALL, orphanRemoval=true) List<Player> players = new ArrayList<Player>(); } @Data @Entity public class Player { @Id @GeneratedValue private Integer id; private String name; @JsonBackReference @ManyToOne @JoinColumn(name = "team_id") private Team team; }
What am I doing wrong? Jackson can match one-on-one relationships when I create a new team with a bunch of players, but when I update / delete players from Team json, he fails with the same error. Is it possible that the collection has been recreated, so hibernate throws an error, if so, how can I fix it? Please, help
source share