-->
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.  [ 5 posts ] 
Author Message
 Post subject: Bidirectional association without an inverse end
PostPosted: Thu Jul 07, 2005 8:22 pm 
Newbie

Joined: Thu Mar 17, 2005 1:55 pm
Posts: 15
What happens if I map a bidirection association, and don't mark either end as inverse? Here's my scenario:

I have a Contact and a ContactGroup managed objects. A ContactGroup can have many contacts, and each contact can be in many groups. So I've mapped a <many-to-many> association on both ends.

The end user will be able to edit this association from both ends: he can either edit a group, and than add users to it, or edit an user, and add groups to it. What I really want is be able to do both:

contact.getGroups().add(someGroup);

OR

group.getContacts().add(someContact);

And have either of those statements update the relationship table at flush time. In other words, I don't want to bother about which end manages the association -- I want both ends to do it. It seems to work right now, but I'm not sure of the consequences. Any words about this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 8:27 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
If you add to both ends, you will get a uniqueness constraint violation (or the row will be inserted twice if you don't have a constraint).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 8:33 pm 
Newbie

Joined: Thu Mar 17, 2005 1:55 pm
Posts: 15
Nebob wrote:
If you add to both ends, you will get a uniqueness constraint violation (or the row will be inserted twice if you don't have a constraint).


Hmm, if this is the only problem, I guess I'll leave the relationship mapped this way. The way the application works, I'll never modify both ends of the relationship in the same transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 8:43 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
I do something similar so that delete-orphan cascades work from both ends of a many-to-many. I haven't found any serious problem with it.

However, for your situation, you'd be better advised writing some addXXX helper methods that automatically set up both ends of the association:

class Bar:

public void addFoo(Foo foo)
{
foos.add(foo);
foo.getBars().add(this);
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 07, 2005 8:56 pm 
Newbie

Joined: Thu Mar 17, 2005 1:55 pm
Posts: 15
Thanks, Nebob. I'll consider adding something like this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.