I have the following pretty simple one to many relations:
Team has a Set of players:
@Entity(name = "TEAM")
@Access(AccessType.PROPERTY)
public class Team{
private Integer id;
private String name;
private Set<Player> players ;
@Id
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "team_name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = {CascadeType.ALL},orphanRemoval=true)
@JoinColumn(name = "TEAM_ID")
public Set<Player> getPlayers() {
return players;
}
public void setPlayers(Set<Player> players) {
this.players = players;
}
}
And each player has a unique id & name.
@Entity(name = "PLAYER")
@Access(AccessType.PROPERTY)
public class Player implements Serializable{
private int id;
private String name;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "player_name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
return id == ((Player)obj).id;
}
@Override
public int hashCode() {
return id;
}
}
I run a very simple code:
Team team = createTeam(3) // creates team with 3 players ids={1,2,3}
session.saveOrUpdate(team);
...
private Team createTeam(int players) {
Team team = new Team();
team.setName("Bears");
team.setId(1);
for(int i=1 ; i<=players; ++ i){
Player player = new Player();
player.setId(i);
player.setName("Player"+i);
team.addPlayer(player);
}
return team;
}
And I get the following as expected:
- Hibernate: select team_.id, team_.team_name as team2_0_ from TEAM team_ where team_.id=?
- Hibernate: select player_.id, player_.player_name as player2_1_ from PLAYER player_ where player_.id=?
- Hibernate: select player_.id, player_.player_name as player2_1_ from PLAYER player_ where player_.id=?
- Hibernate: select player_.id, player_.player_name as player2_1_ from PLAYER player_ where player_.id=?
- Hibernate: insert into TEAM (team_name, id) values (?, ?)
- Hibernate: insert into PLAYER (player_name, id) values (?, ?)
- Hibernate: insert into PLAYER (player_name, id) values (?, ?)
- Hibernate: insert into PLAYER (player_name, id) values (?, ?)
- Hibernate: update PLAYER set TEAM_ID=? where id=? Hibernate: update PLAYER set TEAM_ID=? where id=? Hibernate: update PLAYER set TEAM_ID=? where id=?
Then later I do:
Team team = createTeam(2) // creates team with 2 player ids={1,2}
session.saveOrUpdate(team);
And expect the orphan players to be deleted but I get:
- Hibernate: select team_.id, team_.team_name as team2_0_ from TEAM team_ where team_.id=?
- Hibernate: select player_.id, player_.player_name as player2_1_ from PLAYER player_ where player_.id=?
- Hibernate: select player_.id, player_.player_name as player2_1_ from PLAYER player_ where player_.id=?
- Hibernate: update PLAYER set TEAM_ID=null where TEAM_ID=?
- Hibernate: update PLAYER set TEAM_ID=? where id=?
- Hibernate: update PLAYER set TEAM_ID=? where id=?
Which leaves the orphan player (id=3) disconnected but not deleted... Any ideas what I do wrong?