Hello everybody.
Consider the following simple example: One team has many players and a player may only belong to one team.
@Entity
public class Team {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "team")
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Set<Player> player = new HashSet<Player>();
}
@Entity
public class Player {
@NotNull
@ManyToOne(targetEntity = Team.class)
@JoinColumn(name = "team_id")
private Team team;
}
What I want to achive is moving all players of team A to team B and delete team A afterwards. I do a loop over the players from team A and set their team to team B (here: "this"):
Iterator<Player> iterator = teamA.getPlayer().iterator();
while(iterator.hasNext()){
Player player = iterator.next();
player.setTeam(this);
player.merge();
}
teamA.remove();
The flush is done after that (autoflush), but I also tried to flush before teamA.remove(). This code runs without an error, but afterwards the players from team A are removed from my datasource due to the cascade configuration, as teamA.remove() leads to the removal of all players from team A.
I wonder why they are still associated with team A, because I also tried (and checked in the debugger) to remove all players from the Set, so when teamA.remove() is called, the set is empty. The exceptions thrown in those cases either stated "deleted entity passed to persist" or "detached entity passed to persist". It does work of course, if I create a new transient player, copy all properties and .persist().
How to do this by simply "relinking" the relationship?
Thanks
Wolfram