-->
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.  [ 1 post ] 
Author Message
 Post subject: Updating Child in Bidirectional One to Many
PostPosted: Wed Apr 29, 2015 9:43 am 
Newbie

Joined: Fri Jul 11, 2014 5:36 am
Posts: 2
Hello,

I have a question about one-to-many bidirectional relationships and their inverse many-to-one. I am using Spring JPARespository to make all my DB calls.

I have the following two classes.

Code:
@Entity
public class Member {

    @Id
    @GeneratedValue
    private Long id

    @ManyToOne(cascade = CascadeType.MERGE, optional = false, fetch = FetchType.EAGER)
    private OrgUnit orgUnit;

    public OrgUnit  getOrg() {
        return orgUnit;
    }

    public void setOrg(OrgUnit orgUnit) {
        this.orgUnit = orgUnit;
    }

}


Code:
@Entity
public class OrgUnit{

    @Id
    @GeneratedValue
    private Long id

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<Member> members = new HashSet<Member>();

    public Set<Member> getMembers() {
        return members;
    }

    public void addMember(Member member) {
        this.members.add(member);
        member.setOrg(this);
    }
}


Now, I can add a member to an Org Unit by calling the OrgUnit.addMember(Member) method. If I save that, the relationship in the DB holds. However, if I do the inverse, and assign an Org Unit to a member, the member is never added to the Org Unit's member list.

Do I have to explicitly maintain these relationships like I have in the Org Unit class (Where I set the Member's org unit to be 'this' and add it to the Member collection) or should that be maintained by the DB for me?

Thank you for any help.


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

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.