简体   繁体   中英

How delete object in one side at bidirectional relation?

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.

Use This way for deleting only one side in one to many or many to one relationship.

@ManyToOne(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY)
//  @JoinColumn(name = "qid")
    @JoinColumn(name = "qid", referencedColumnName = "qid", foreignKey = @ForeignKey(name = "qid"), nullable = false)
    // @JsonIgnore
    @JsonBackReference
    private QueueGroup queueGroup;

Here CascadeType is the main thing all the transaction depends on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM