It looks like the problem may have to do with the way you try to set the
bi-directional relationship:

In your test code, you call addItem on the Channel instance.  Then in that
method, you call setChannel on the incoming Item instance.  The setChannel
method, however, referes to an instance variable named channel, not the
channels collection which is what is really mapped to Castor.

Try the following change:

In the Channel object, change the method addItem from:
public void addItem( Item item ) 
{
    items.addElement( item );
    item.setChannel( this );
}

to:
public void addItem( Item item ) 
{
    items.addElement( item );
    item.addChannel( this );
}

However, be sure to avoid circular references. For example,
public class Channel
{
    public void addItem( Item item )
    {
        ...
        item.addChannel( this );
    }
}

public class Item
{
    public void addChannel( Channel channel )
    {
        ...
        channel.addItem( this );
    }
}

The best way to avoid is either to set this relationship explicitly outside
of the classes themselves, or to determine a "required" persistence path and
only set the relationship that direction.

HTH


********************************************
Steve Ebersole
IT Integration Engineer
Vignette Corporation 
512.741.4195

Visit http://www.vignette.com

********************************************


-----Original Message-----
From: Pascal Gheeraert [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 10, 2002 7:36 AM
To: [EMAIL PROTECTED]
Subject: [castor-dev] Problems with the many-to-many relation :
association table keeps empty


I've problems with the many-to-many relation using Castor JDO. Can somebody
explain me why my assocaition table keeps empty.

In fact, here is the situation :
I want to set a many-to-many relation between 2 class : Channel and Item.
(A Channel can have 0 or more Items and an Item can belong to 0 or more
Channels)

I've tested the one-to-many relation and it works fine (each Item has a
foreign key on channel).

Here is my xml mapping file to test the many-to-many relation:

<class name="com.ibm.essonnes.castor.jdoTestSuite.rssDef.Channel"� identity
="id">
��� <description>Channel</description>
��� <cache-type type="unlimited"/>
��� <map-to table="channel" />
��� <field name="id" type="integer">
��� <sql name="id" type="integer" />
��� </field>
...
��� <field name="items" type
="com.ibm.essonnes.castor.jdoTestSuite.rssDef.Item" required="true"
collection="vector">
��� <sql name="item_id" many-table="channel_item" many-key="channel_id" />
��� </field>
</class>

<class name="com.ibm.essonnes.castor.jdoTestSuite.rssDef.Item" identity
="id" >
��� <description>Item </description>
��� <cache-type type="unlimited"/>
��� <map-to table="item" />
��� <field name="id" type="integer">
��� <sql name="id" type="integer" />
��� </field>
...
��� <field name="channels" type
="com.ibm.essonnes.castor.jdoTestSuite.rssDef.Channel" required="true"
collection="vector">
��� <sql name="channel_id" many-table="channel_item" many-key="item_id" />
��� </field>
</class>

My java Class contains:
* in Channel.java:

public void setItem(Item item) {
this.item = item;
}
public Item getItem() {
return item;
}
public void setItems(Vector items) {
this.items=items;
}
public Vector getItems() {
return items;
}
public void addItem( Item item ) {
items.addElement( item );
item.setChannel( this );
}

*In Item.java:
public Channel getChannel() {
return channel;
}
public void setChannel( Channel channel ) {
this.channel = channel;
}
public Vector getChannels() {
return channels;
}
public void setChannels(Vector channels) {
this.channels = channels;
}
public void addChannel(Channel channel) {
channels.addElement(channel);
channel.setItem(this);
}

* to test, I wrote :
...
for (int i = 1; i <= 5; i++) {
Channel channel = new Channel();
channel.setId(i);
for (int j=1;j<=10;j++) {
Item item = new Item();
item.setId(j);
item.addChannel(channel);
db.create(item);
channel.addItem(item);
}
db.create(channel);
db.commit();
}
...
My trouble is that my association table (channel_item) contains�0 values
after execution; and no errors are reported.

I hope someone of you�have an�Idea and will take some of his time to answer
me,
Thanks a lot
Coridialement/Regards
Pascal Gheeraert

PS : I work with the DB2 database.

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to