Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How delete object in one side at bidirectional relation?

I want delete relation from one side. Let me explain.

My database looks like this :enter image description here

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!

like image 956
CeZet Avatar asked Jan 22 '16 12:01

CeZet


1 Answers

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.

like image 188
Alan Hay Avatar answered Oct 21 '22 19:10

Alan Hay