-->
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: Updating Bidirectional many-to-many Associations
PostPosted: Mon Sep 10, 2012 1:10 pm 
Newbie

Joined: Thu Apr 14, 2011 4:11 am
Posts: 6
Hi everyone,

in my project, we have manytomany associations of the form:

Code:
class Booking {
List<Wagon> wagons;
}

class Wagon {
List<Bookings> bookings;
}


I know that one side of a manytomany relation is the owning side and only
this side is used to update the association table.
Though updating one side of the association is enough, we would like to keep the object state consistent with the database
state to avoid misinterpretations.

As far as I know, in onetomany relations, the one-side's methods update the relation in both sides by convention such as:
Code:
class Person {
   addDog(Dog d) {
      mydogs.add(d);
      d.setOwner(this);
   }
}



Is there any similar convention to manage the object state of manytomany relations?

What I can think of is:

Code:
//owning side
class Booking {
   List<Wagon> wagons;

   addWagonsBidirectional(Wagon wg) {
     wagons.add(wg);
     wg.getBookings.add(this);
   }
}

//inverse side
class Wagon {
   List<Bookings> bookings;

   getBookings() {
      return bookings;
   }
}

In that case we would (by convention) alwaws use the owning side as
responsible side for keeping relation state correct.

Many thanks in advance!


Top
 Profile  
 
 Post subject: Re: Updating Bidirectional many-to-many Associations
PostPosted: Sat Sep 15, 2012 1:08 pm 
Beginner
Beginner

Joined: Fri Sep 14, 2012 10:41 am
Posts: 20
You have it right. In the many-to-many association, the owing side should be used to persist.

You seem to know the solution, then I don't understand your question.


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.