Cześć, mam dwie klasy, encje :
@Entity
public class Player
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String nick;
@ManyToOne
private Team team;
}
i
@Entity
public class Team
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Player> players = new ArrayList<>();
@Transient
public void addPlayer(Player player)
{
if (player != null)
players.add(player);
}
}
gdzieś tam w kontrolerze chcę sobie utworzyć po 1 obiekcie danej klasy i je połączyć dwukierunkowo:
private void addTeams()
{
Team team = new Team();
team.setName("Name");
Player p = new Player();
team.addPlayer(p); // wstawia nulla do team_id w bazie danych
// p.addTeam(team); // wstawia dobry klucz do team_id
teamsService.addTeam(team);
playersService.addPlayer(p);
}
I teraz dziwna sprawa.. jeśli łączę przez player.addTeam(team) to działa, tzn zapisuje się w bazie danych w kolumnie team_id odpowiedni klucz. A jeśli zrobię team.addPlayer(player) to wstawia nulla. Już naprawdę nie wiem co tu jest grane.
Robię identycznie jak tu :
@Shalom