I want delete relation from one side. Let me explain.
My database looks like this :
I based my repositories on JPARepository
.
and I have two entities Team
and Player
.
Player
Entity looks like this:
@Entity
@Table(name = "PLAYER")
public class Player implements Serializable, Comparable<Player>{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id_player;
@Size(min=3, max=20)
private String name;
@Size(min=3, max=20)
private String surname;
@Size(min=3, max=20)
private String position;
@Min(value=10)
private int age;
@Min(value=1)
private int number;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_team")
private Team team;
//getters and setters
}
...and Team
entity:
@Entity
@Table(name = "TEAM")
public class Team implements Serializable, Comparable<Team>{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id_team;
@Size(min=4, max=20)
private String name;
@Size(min=4, max=20)
private String city;
private int goals_hit;
private int goals_lost;
private int points;
private int ranked;
@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "id_coach")
private Coach coach;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_stadium")
private Stadium stadium;
@OneToMany(mappedBy = "team", fetch = FetchType.LAZY)
private List<Player> playerList;
@OneToMany(mappedBy = "team", fetch = FetchType.LAZY)
private List<TeamChoosenDate> teamChoosenDateList;
//getters and setters
}
So Player
contains one relation to Team
but Team
contains a List
of Players
.
Coach can edit his team and for example he wants delete Player
from Team
(relation between Team
--> Player
) but not lose the Player
(because he has relations with other table - Events
).
In conclusion:
Delete Player
from correct Team
, but NOT LOSE Player
object.
How can I do this ?
My try: I delete Player
from List
but every time when I reload - he comes back... Below method shows how I do this :
public void deletePlayerFromTeamPlayerList(Long idTeam, int idPlayer){
Team team = teamRepository.findOne(idTeam);
Player playerTmp = playerRepository.findOne( (long) idPlayer);
List<Player> playerList;
int indexOfFindedPLayer;
playerList = team.getPlayerList();
System.out.println("Before Delete: " + playerList.toString());
indexOfFindedPLayer = playerList.indexOf(playerTmp);
if(indexOfFindedPLayer >= 0){
playerList.remove(indexOfFindedPLayer);
System.out.println("After Delete: " + playerList.toString());
team.setPlayerList(playerList);
teamRepository.save(team);
}
}
But when the element is removed, at Player
object id_team
is still the same (ex. id_team = 2)
Thanks for all suggestions!
You can achieve this more simply by doing the below:
public void deletePlayerFromTeam(int idPlayer){
Player player = //loadPlayer;
player.setTeam(null);
playerRepository.save(player);
}
You can also achieve this in your original code by setting the player.team to null and and by setting cascade on the collection to merge:
@OneToMany(mappedBy = "team", fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
private List<Player> playerList;
You should always ensure both sides of the relationship are maintained consistently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With