views:

446

answers:

1

Hi,

I don't manage to persist a many-to-many link with DataNucleus using JDO. I have two classes Book and Shop. This is the orm mapping file:

<?xml version="1.0"?>
<!DOCTYPE orm PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN" 
    "http://java.sun.com/dtd/orm_2_0.dtd"&gt;

<orm>
    <package name="com.mypackage.pojo">
        <class name="Book" identity-type="datastore">
            <datastore-identity>
                <column name="BOOK_ID" />
            </datastore-identity>

            <field name="name">
                <column length="100" jdbc-type="VARCHAR" />
            </field>

            <field name="shops" persistence-modifier="persistent"
                    table="BOOKS_SHOPS">
                <collection element-type="com.mypackage.pojo.Shop" />
                <join>
                    <column name="BOOK_ID" />
                </join>
                <element>
                    <column name="SHOP_ID" />
                </element>
            </field>
        </class>

        <class name="Shop" identity-type="datastore">
            <datastore-identity>
                <column name="SHOP_ID" />
            </datastore-identity>

            <field name="name">
                <column length="50" jdbc-type="VARCHAR" />
            </field>

            <field name="books" persistence-modifier="persistent" 
                    table="BOOKS_SHOPS">
                <collection element-type="com.mypackage.pojo.Book" />
                <join>
                    <column name="SHOP_ID" />
                </join>
                <element>
                    <column name="BOOK_ID" />
                </element>
            </field>
        </class>
    </package>
</orm>

I try to link a book to a shop and the other way around, like this:

shop.addBook(book);
book.addShop(shop);

Making these two objects persistent again doesn't do anything. Both before and after the little code snippet above, their ObjectState is detached-clean.

What could I be doing wrong?

A: 

I have a working solution, but have to admit I do not fully understand everything. Things work when the last field element is not defined as

<field name="books" persistence-modifier="persistent" table="BOOKS_SHOPS">

but as

<field name="books" persistence-modifier="persistent" mapped-by="shops">

This solved my problem.

See also the DataNucleus manual on JDO M-N Relationships. However, leaving the join and the element elements out, as is done in this example, didn't work for me. Another relevant link on the DataNucleus site is JDO Guides : M-N Relation. The code for this last example can be found on SourceForge. Unfortunately, this example also didn't work for me.

This is not really a good answer, but all that I have to offer for now...

Bno
Since "mapped-by" is needed to interrelate the two sides of the relation ... aka bidirectional.
DataNucleus