In the FAQ it says that under the ODMG API, "Objects are registered to
a transaction so that on commit of the transaction it knows" to
properly store modified elements.

I'm having trouble even getting the first stage of this accomplished.
I've modified tutorial 1 to add a discount to the product and I've
added to the Product class a list of product categories to which the
product belongs.  This works fine in tutorial 1.  I'm able to enter
multiple categories for a product and list them.

The schema for both tutorial 1 and 2 looks like this:

  <table name="PRODUCT">
    <column name="ID" required="true" primaryKey="true" type="INTEGER"
            javaName="_id"/>
    <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"
            javaName="_id"/>
    <column name="NAME" type="VARCHAR" size="100"/>
  </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>

The repository_user.xml file looks like this:

  <class-descriptor class="org.apache.ojb.tutorial1.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="org.apache.ojb.tutorial1.ProductCategoryName"
          auto-retrieve="true"
          auto-update="true"
          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="org.apache.ojb.tutorial1.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"/>
  
      <collection-descriptor
          name="products"
          element-class-ref="org.apache.ojb.tutorial1.Product"
          auto-retrieve="true"
          auto-update="false"
          indirection-table="PRODUCT_CATEGORY">
          <fk-pointing-to-this-class column="PRODUCT_CATEGORY_NAME_ID"/>
          <fk-pointing-to-element-class column="PRODUCT_ID"/>
      </collection-descriptor>
  </class-descriptor>
  
  <class-descriptor class="org.apache.ojb.tutorial1.ProductCategory"
      table="PRODUCT_CATEGORY">
      <field-descriptor id="1" name="id" column="PRODUCT_ID"
          jdbc-type="INTEGER" primarykey="true"/>
      <field-descriptor id="2" name="id"
      column="PRODUCT_CATEGORY_NAME_ID"
          jdbc-type="INTEGER" primarykey="true"/>
  
      <reference-descriptor
          name="product"
          class-ref="org.apache.ojb.tutorial1.Product">
          <foreignkey field-id-ref="1"/>
      </reference-descriptor>
  
      <reference-descriptor
          name="productCategoryName"
          class-ref="org.apache.ojb.tutorial1.ProductCategoryName">
          <foreignkey field-id-ref="2"/>
      </reference-descriptor>
  </class-descriptor>

I have gotten this to work in tutorial 1 UCEnterNewProduct.java
apply() by doing this (comments, try/catch, and a few lines removed
for clarity):

        Product newProduct = new Product();

        String in = readLineWithMessage("enter name:");
        newProduct.setName(in);

        // ...
        // enter and set price, discount, and stock
        // ...

        in = readLineWithMessage("enter product category:");
        while (0 != in.length()) {
            ProductCategoryName productCategoryName = new
                ProductCategoryName();
            productCategoryName.setName(in);
            in = readLineWithMessage("enter product category:");
            newProduct.addProductCategoryName(productCategoryName);
        }

        broker.beginTransaction();
        broker.store(newProduct);
        broker.commitTransaction();

Product looks like this:

        public class Product implements java.io.Serializable {
            private int id;

            protected double discount;

            protected double price;

            protected int stock;

            protected String name;

            Vector productCategoryNames = new Vector();

            // ...
            // all appropriate get/set methods elided
            // ...
        }

ProductCategoryName looks like this:

        public class ProductCategoryName implements java.io.Serializable {
            private int id;

            protected String name;
        
            Vector products = new Vector();
        
            // ...
            // all appropriate get/set methods elided
            // ...
        }

and ProductCategory looks like this:

        public class ProductCategory implements java.io.Serializable {
            private int productID;

            private int productCategoryNameID;

            private Product product;

            private ProductCategoryName productCategoryName;

            // ...
            // all appropriate get/set methods elided
            // ...
        }

However, I have modified tutorial 2 along the same lines, and have
changed UCEnterNewProduct.java to:

    public void apply() {
        // get product from user as in tutorial 1
        Product newProduct = inputProduct();

        Transaction tx = odmg.newTransaction();

        tx.begin();
        tx.lock(newProduct, Transaction.WRITE);
        tx.commit();
    }

But this does not seem to work.  I read through
odmg/ManyToManyTest.java which has Gourmet objects storing a list of
Food objects, and saw that a Database object was being used to store
the objects.

If I try getting the Database object from the Application and using it
thus:

    public void apply() {
        // get product from user as in tutorial 1
        Product newProduct = inputProduct();

        Transaction tx = odmg.newTransaction();

        tx.begin();
        db.makePersistent(newProduct);
        tx.commit();
    }

it doesn't work either.

I am running this against MySQL.

Sorry for the newbie question, but can anyone help?


Bill

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

Reply via email to