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 Martin Easterbrook <ua...@dircon.co.uk> on 2004/08/08 20:22:38 UTC
Using Inner Class Collection types (Newbie asking for help)
Hi,
I wonder if anyone can help me with this.
I have some code which uses inner class collections
because they seem to be an 'easy' way to implement
bi-directional links. This means that I can write;
theFoo.tobar.set(theBar);
// and then
Collection foos = theBar.toFoo.CopySet();
// will contain 'theFoo'
// or
theBar.toFoo.add( theFoo2);
// and
theBar == theFoo2.toBar.get(); // will now be true
I have included the class definitions for foo and bar below.
I've omitted the link superclasses for brevity but if anyone
wants a look at them just e-mail me.
These are just simple example implementation classes and I hope
to use these as templates to generate 2 way links from
a datamodel xml schema or xmi model.
My questions are;
1. Is there an easier way of implementing bi-directional links with OJB.
2. I have seen bi-directional links mentioned in discussions of JDO 2.
Will JDO 2 have a standard way of doing this ?
3. (And most important). How can I produce Xdoclet annotations or a
repository.xml to enable the fields 'toFoo' or 'ToBar' below to be
mapped
properly in OJB.
I'd be very grateful to anyone who can provide me with some pointers
of how to proceed.
Many Thanks
Martin Easterbrook
================================================
public class foo extends Element
{
/*
* Create an inner class which can return the reverse 'to many'
association
* and returns the link data for the association from any object of type
'foo'.
*/
protected static final class tobarFieldAccessor implements
ToOneFieldAccessor
{
public ToManyFieldAccessor getInverseToManyFieldAccessor()
{
return bar.tofooAssociation;
}
public ToOneLinkData getToOneLinkData(Element e)
{
return ((foo)e).tobar ;
}
}
/*
* Create an inner class which 'types' the 'get' and 'set' operations.
*/
public static final class ToOneBarLinkData extends ToOneLinkData
{
public ToOneBarLinkData(Element owner, ToOneFieldAccessor
p_toOneFieldAccessor )
{
super( owner, p_toOneFieldAccessor);
}
public void set( bar b) { super.setElement( b ); }
public bar get() { return ( bar) super.getElement() ;}
}
/*
* create a static instance of the 'to one' association which can be
accessed
* by the 'to many' at the other end.
*/
protected static final ToOneFieldAccessor tobarAssociation = new
tobarFieldAccessor();
/*
* create an instance of the 'to one' association data for each instance
of the class
*/
public final ToOneBarLinkData tobar = new
ToOneBarLinkData(this,tobarAssociation);
public foo(String p_name)
{
super(p_name);
}
}
//====================================
public class bar extends Element
{
/*
* Create an inner class which can return the reverse 'to one'
association
* and returns the link data for the association from any object of type
'bar'.
*/
protected static final class toFooFieldProvider implements
ToManyFieldAccessor
{
public ToOneFieldAccessor getInverseToOneFieldAccessor()
{
return foo.tobarAssociation;
}
public ToManyLinkData getToManyLinkData(Element e) { return
((bar)e).toFoo ; }
}
/*
* Create an inner class which 'types' the 'add' and 'remove' operations.
*/
public static final class ToManyFooLinkData extends ToManyLinkData
{
public ToManyFooLinkData(Element owner, ToManyFieldAccessor
p_toManyFieldAccessor)
{
super( owner , p_toManyFieldAccessor);
}
public void add(foo e) { super.addElement( e ) ;}
public void remove(foo e) { super.removeElement( e ) ;}
}
/*
* create a static instance of the 'to many' association which can be
accessed
* by the 'to one' at the other end.
*/
protected static ToManyFieldAccessor tofooAssociation = new
toFooFieldProvider();
/*
* create an instance of the 'to many' association data for each instance
of the class
*/
public final ToManyFooLinkData toFoo = new
ToManyFooLinkData(this,tofooAssociation);
public bar(String p_name)
{
super(p_name);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org