Many-to-Many relationship in DataNucleus (JDO) is not preserved

I am unable to save the many-to-many link using DataNucleus using JDO. I have two classes Bookand 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">

<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 am trying to link a book to a store and vice versa, for example:

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

Restoring these two objects does nothing. Both before and after the small code snippet above, they ObjectStateare detached-clean.

What can i do wrong?

+3
source share
1 answer

, , . , field

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

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

.

. DataNucleus JDO M-N. , join element, , . DataNucleus JDO Guides: M-N Relation. SourceForge. , .

, , ...

+4

Source: https://habr.com/ru/post/1714761/


All Articles