Results 1 to 2 of 2

Thread: Automatically refresh parent when merge on child?

  1. #1
    Join Date
    Sep 2009
    Posts
    1

    Default Automatically refresh parent when merge on child?

    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.

    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.

  2. #2
    Join Date
    Sep 2009
    Posts
    9

    Default

    I am not sure about your refreshing problem but one think looks incorrect to me is mapping, one side you have defined one-to-one mapping while other side you have defined one-to-many,
    this style of mapping is typically recommended for bidirectional one-to-one mapping with many-to-one side of mapping set to unique.

    Please make sure you have mapped as desired

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •