-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Automatically refresh parent when merge on child?
PostPosted: Tue Sep 29, 2009 10:43 am 
Newbie

Joined: Tue Sep 29, 2009 10:13 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Automatically refresh parent when merge on child?
PostPosted: Tue Sep 29, 2009 5:12 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
I guess you are missing the "inverse" attribute. "inverse" attibute tells hibernate which end of the association that hibernate needs to synchronize with the database. Please see this link.
http://docs.jboss.org/hibernate/core/3. ... child.html

-Srilatha.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.