I have an app with Angular front end and Spring backend. The two classes in question here are (backend):
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "tournament_games")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class TournamentGame {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "code", foreignKey = @ForeignKey(name = "code_fk"))
private TournamentCode code;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "type", foreignKey = @ForeignKey(name = "game_type_fk"))
private GameType type;
@Column(name = "home_score")
private int home_score;
@Column(name = "away_score")
private int away_score;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "result_type", foreignKey = @ForeignKey(name = "result_type_fk"))
private ResultType result_type;
@Column(name = "status")
private boolean status;
@Column(name = "round")
private int round;
@Column(name = "locked")
private boolean locked;
@OneToMany(mappedBy = "game", fetch = FetchType.EAGER)
private List<TournamentGamesPlayers> players = new ArrayList<>();
}
and
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "tournament_games_players")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "game")
public class TournamentGamesPlayers implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@ManyToOne
@JoinColumn(name = "tournament_game_id")
private TournamentGame game;
@Id
@ManyToOne
@JoinColumn(name = "playerid")
private Player player;
@Column(name = "home")
private boolean home;
}
I need help figuring out how to persist the List<TournamentGamesPlayers>
when I save and/or update a TournamentGame
object. I generate 45 games. The first 30 games have known players, and so I set them before saving. The last 15 do not have entries for the TournamentGamesPlayers
join table, because I need to add them later.
I am able to get some results with CascadeType.ALL
on the @OneToMany
side when I initially generate the games, but it fails when I try to update a game with a seemingly infinite recursion/stack overflow.
If I omit any cascade type, the games side get generated, but the join table @ManyToOne
side does not get entered.