How can I remove one entity from its parent and add it to another parent?
Here is my code. Team has a List<Player>, and Player has an association to its parent:
Player player = entityManager.find(Player.class, playerKey);
Team team1 = player.getTeam();
team1.getPlayers().remove(player); // this action will cascade the deletion of player;
entityManager.merge(team1);
Team team2 = entityManager.find(Team.class, team2Key);
team2.getPlayers().add(player);
entityManager.merge(team2);
This is what I want to do, but will result in failure. I thought I could use entityManager.detach(player) to use in the other team, but this method is not available in the GAE jar (?).
You mean this is a GAE “owned” relation? In which case you likely can’t change the “owner” since GAE will have put it into the Key of the Player.
If instead you make the relation as “unowned” (like all relations for all other datastores) you can obviously reparent it. You need to be using GAE/J JPA plugin v2.x for this.
And yes GAE/J JPA does support JPA2, if you’re using the GAE/J JPA plugin v2.x (with DataNucleus 3.x)