I've been struggling trying to get an m:n relationship set up.  It is
a simple variation on the tutorial.  I have added a
PRODUCT_CATEGORY_NAME table and I relate this to a PRODUCT through a
PRODUCT_CATEGORY table.

I've been chasing this for quite a while, and I've searched through
the archives, but I can't seem to figure out how to express this both
in the schema and the repository files.

the schema looks like this:

    <table name="PRODUCT">
        <column name="ID" required="true" primaryKey="true"
        type="INTEGER"/>
        <column name="NAME" type="VARCHAR" size="100"/>
        <column name="PRICE" type="FLOAT"/>
        <column name="DISCOUNT" type="FLOAT"/>
        <column name="STOCK" type="INTEGER"/>
    </table>

    <table name="PRODUCT_CATEGORY_NAME">
        <column name="ID" required="true" primaryKey="true"
        type="INTEGER"/>
        <column name="NAME" type="VARCHAR" size="48"
                      primaryKey="true"/>

        <unique>
            <unique-column name="NAME"/>
        </unique>
    </table>

    <table name="PRODUCT_CATEGORY">
        <column name="PRODUCT_ID" required="true" primaryKey="true"
                type="INTEGER"/>
        <column name="PRODUCT_CATEGORY_NAME_ID" required="true"
                primaryKey="true" type="INTEGER"/>

        <foreign-key foreignTable="PRODUCT">
            <reference local="PRODUCT_ID" foreign="ID"/>
        </foreign-key>
        <foreign-key foreignTable="PRODUCT_CATEGORY_NAME">
            <reference local="PRODUCT_CATEGORY_NAME_ID" foreign="ID"/>
        </foreign-key>
    </table>

and my repository:

<class-descriptor class="com.zopyra.test.tutorial2.Product"
table="PRODUCT">
    <field-descriptor id="1" name="id" column="ID"
        jdbc-type="INTEGER" primarykey="true" autoincrement="true"/>

    <field-descriptor id="2" name="name" column="NAME"
        jdbc-type="VARCHAR"/>

    <field-descriptor id="3" name="price" column="PRICE"
        jdbc-type="DOUBLE"/>

    <field-descriptor id="4" name="discount" column="DISCOUNT"
        jdbc-type="DOUBLE"/>

    <field-descriptor id="5" name="stock" column="STOCK"
        jdbc-type="INTEGER"/>

    <collection-descriptor
        name="productCategoryNames"
        element-class-ref="com.zopyra.test.tutorial2.ProductCategoryName"
        indirection-table="PRODUCT_CATEGORY">
        <fk-pointing-to-this-class column="PRODUCT_ID"/>
        <fk-pointing-to-element-class
        column="PRODUCT_CATEGORY_NAME_ID"/>
    </collection-descriptor>
</class-descriptor>

<class-descriptor class="com.zopyra.test.tutorial2.ProductCategoryName"
    table="PRODUCT_CATEGORY_NAME">
    <field-descriptor id="1" name="id" column="ID"
        jdbc-type="INTEGER" primarykey="true" autoincrement="true"/>

    <field-descriptor id="2" name="name" column="NAME"
        jdbc-type="VARCHAR" primarykey="true" nullable="false"/>
</class-descriptor>

I would like to be able to create a Product object, populate it with
values, among them being the list of category names that represent
it.  I would like then to save this to the DB by saving just the
product object.  I would like also to be able to declare another
Product object, for example, populate it with two category names, one
of which might already be in the PRODUCT_CATEGORY_NAME table.  When I
save this new product object, I would like it to recognize that one of
its values is already accounted for in the PRODUCT_CATEGORY_NAME
table, and to just enter the new product category name there, and make
two entries in the PRODUCT table.

I have tried another variation (which I forget at the moment) that
allowed me to insert values, but fails when I try to update with a new
Product which has category names that already exist.

If I can't express this "automatically" through OJB, how can I do it?

Surely someone has run in to this.

Sorry for the long post and thanks for any help you can offer.


Bill

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to