You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Tino Schöllhorn <t....@tiscali.de> on 2003/12/16 00:03:51 UTC

Design Pattern for referential integrity

Hi there,

this is a topic which is not specifically bound to ojb - but since there 
are a lot of users out there who might have the same problem (and 
hopefully a solution) I post this message here:

I have several classes which I use to map tables to objects. Now I am 
wondering what the most appropriate design pattern for "referential 
integrity" is. An example. Suppose we have two classes which implement a 
N:M-Relation:

class A {
	Collection bs;
	
	void addB(B b) {
		bs.add(b);
	}

	void removeB(B b) {
		bs.remove(b);
	}
}

// the class B is the same as above.

Now I'd like to use these classes. But there is one problem: When using 
these simple classes I have to write:

A a = new A();
B b = new B();
a.addB(b);
b.addA(a);

so that one can navigate from both instances to each other. But I juest 
want to write:

a.addB(b);

And the changes should also be reflected in object b.

Does anyone know a design pattern for this (quite common) problem?

Tino



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Design Pattern for referential integrity

Posted by Brian McCallister <mc...@forthillcompany.com>.
I generally do soemthing like:

class A
{
	Set bs;
	...
	public void addB(B b)
	{
		ibs.add(b);
		if (! b.getAs().contains(this)) b.addA(this);
	}
}

-Brian

On Dec 15, 2003, at 6:03 PM, Tino Schöllhorn wrote:

> Hi there,
>
> this is a topic which is not specifically bound to ojb - but since 
> there are a lot of users out there who might have the same problem 
> (and hopefully a solution) I post this message here:
>
> I have several classes which I use to map tables to objects. Now I am 
> wondering what the most appropriate design pattern for "referential 
> integrity" is. An example. Suppose we have two classes which implement 
> a N:M-Relation:
>
> class A {
> 	Collection bs;
> 	
> 	void addB(B b) {
> 		bs.add(b);
> 	}
>
> 	void removeB(B b) {
> 		bs.remove(b);
> 	}
> }
>
> // the class B is the same as above.
>
> Now I'd like to use these classes. But there is one problem: When 
> using these simple classes I have to write:
>
> A a = new A();
> B b = new B();
> a.addB(b);
> b.addA(a);
>
> so that one can navigate from both instances to each other. But I 
> juest want to write:
>
> a.addB(b);
>
> And the changes should also be reflected in object b.
>
> Does anyone know a design pattern for this (quite common) problem?
>
> Tino
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org