I have a parent/child relationship such that the parent contains a List of child elements. I am trying to get it so when a child element is updated, the parent's List of child elements is automatically updated. So far, I can't get this to work - it will only update the parent List on the
second update of a child. I have tried CascadeType.ALL/CascadeType.REFRESH on both the child and the parent but neither seem to work. However, if I update the child element, then call em.refresh(parent), it works properly, or if I execute a query to retrieve the children (FROM child WHERE parent_id = ?), it works properly as well. But I would like this to happen automatically, to save me some typing and also because I think this is how it is supposed to work.
Here is the child:
Code:
public class Player implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
@Temporal(TemporalType.DATE)
private Date birthDate;
@OneToOne(cascade=CascadeType.REFRESH)
private Team team;
...
and here is the parent:
Code:
public class Team implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int wins;
private int losses;
@OneToMany(mappedBy="team",cascade = CascadeType.ALL)
private List<Player> players;
I am trying to change the Team of a Player by changing player.team to NULL, removing the Player from the team. The weird thing is that changing Player.team to a certain team (adding a player to a team) works fine, and the change is shown immediately, but when REMOVING a player from a team (changing Player.team to NULL or another team) the change doesn't happen until the NEXT change to a player is made. BUT, after removing a player from a team, executing a query to retrieve a list of players on the team returns the correct list, but getting a list of players through Team.getPlayers() does not.
Any ideas?
...Let me know if you need to see any more code/if this wasn't clear.