Hi,

Rajesh Bahtia wrote:
> 
>> -----Original Message-----
>>From:         Rajesh Bahtia  
>>Sent: Friday, August 23, 2002 9:14 PM
>>To:   '[EMAIL PROTECTED]'
>>Subject:      Collection Persistence
>>
>>I have 2 tables
>>a. Product
>>b. Product_Attr
>>
>>I have the Schemas defined at the end of this mail.
>>
>>Product is a 1:n relation with Product_Attr
>>
>>I need to automatically edit/update the Product_Attr, when I edit/update
>>the Product.
>>
>>Following is the code snippet that does my update to Product and tries to
>>tell it to Persist the Product_Attr. (This code only updates the Product
>>table and not the Product_Attr table.)
>>
>>--------------------------------------------------------------------------
>>--------------------------------------------------------------------------
>>-------------------------------------------------------
>>
>>            // 2. start transaction
>>            tx = odmg.newTransaction();
>>            tx.begin();
>>
>>            // 3. lookup the product specified by query
>>            OQLQuery query = odmg.newOQLQuery();
>>            query.create(oqlQuery);
>>            DList result = (DList) query.execute();
>>            Product toBeEdited = (Product) result.get(0);
>>
>>            // 4. lock the product for write access
>>            tx.lock(toBeEdited, tx.WRITE);
>>
>>            // 5. Edit the product entry
>>            in = readLineWithMessage("enter name (was " +
>>toBeEdited.getNAME() + "):");
>>            toBeEdited.setNAME(in);
>>            in = readLineWithMessage("enter price (was " +
>>toBeEdited.getPRICE() + "):");
>>            toBeEdited.setPRICE(new java.math.BigDecimal(in));
>>            in = readLineWithMessage("enter the desc (was " +
>>toBeEdited.getDESCRIPTION() + "):");
>>            toBeEdited.setDESCRIPTION(in);
>>
>>            Vector productattr = toBeEdited.getCollProduct_attr();
>>            Vector modifiedproductattr = new Vector();
>>            Iterator productattrIterator = productattr.iterator();
>>            while (productattrIterator.hasNext()) {
>>              Product_attr pattr =
>>(Product_attr)productattrIterator.next();
>>              System.out.println("Changing the Product Attr Value from
>>"+pattr.getVALUE()+" to Hey");
                 // 1. solution: register attributes too !
                tx.lock(pattr, tx.WRITE);

>>              pattr.setVALUE("Hey");
>>              modifiedproductattr.add(pattr);
>>            }
>>            toBeEdited.setCollProduct_attr(modifiedproductattr);
>>
>>            // 6. commit transaction
>>            tx.commit();
>>
>>--------------------------------------------------------------------------
>>--------------------------------------------------------------------------
>>-----------------------------------------------------
>>
>>The stmt " toBeEdited.setCollProduct_attr(modifiedproductattr); " does not
>>seem to have any affect bcs the Product_Attr does not change the Value to
>>"Hey" in the DataBase. The Product table changes to the correct values
>>that the user chooses from the database.
>>

2. Solution: Don't use Collection or Vector type for your 
collProduct_attr. Use org.odmg.DList instead. you have to declare DList 
in the Collection-descriptor with the attribute collection-class too.

cheers,
Thomas


>>
>>Is this the correct way to tell my code to persist the product_attr
>>relation when updating the product.
>>
>>Thanks
>>Rajesh
>>
>>PS: -
>>The Schema for these 2 tables are as follows
>>
>>  <class-descriptor class="org.apache.ojb.channelx.Product"
>>table="KALYAN.PRODUCT">
>>    <field-descriptor id="2" name="dESCRIPTION" column="DESCRIPTION"
>>jdbc-type="VARCHAR"/>
>>    <field-descriptor id="0" name="kEY" column="KEY" jdbc-type="DECIMAL"
>>primarykey="true"/>
>>    <field-descriptor id="1" name="nAME" column="NAME"
>>jdbc-type="VARCHAR"/>
>>    <field-descriptor id="3" name="pRICE" column="PRICE"
>>jdbc-type="DECIMAL"/>
>>    <collection-descriptor name="collProduct_attr"
>>element-class-ref="org.apache.ojb.channelx.Product_attr"
>>auto-retrieve="true" auto-update="true" auto-delete="false">
>>      <inverse-foreignkey field-id-ref="2"/>
>>    </collection-descriptor>
>>  </class-descriptor>
>>
>>  <class-descriptor class="org.apache.ojb.channelx.Product_attr"
>>table="KALYAN.PRODUCT_ATTR">
>>    <field-descriptor id="1" name="pRODUCT_ATTR_MASTER_KEY"
>>column="PRODUCT_ATTR_MASTER_KEY" jdbc-type="DECIMAL"/>
>>    <field-descriptor id="3" name="pRODUCT_ATTR_TYPE_KEY"
>>column="PRODUCT_ATTR_TYPE_KEY" jdbc-type="DECIMAL"  />
>>    <field-descriptor id="2" name="pRODUCT_KEY" column="PRODUCT_KEY"
>>jdbc-type="DECIMAL" primarykey="true"/>
>>    <field-descriptor id="0" name="vALUE" column="VALUE"
>>jdbc-type="VARCHAR"/>
>>    <reference-descriptor name="aProduct"
>>class-ref="org.apache.ojb.channelx.Product" auto-retrieve="true"
>>auto-update="true" auto-delete="false">
>>      <foreignkey field-id-ref="2"/>
>>    </reference-descriptor>
>>    <reference-descriptor name="aProduct_attr_type"
>>class-ref="org.apache.ojb.channelx.Product_attr_type"
>>auto-retrieve="false" auto-update="false" auto-delete="false">
>>      <foreignkey field-id-ref="3"/>
>>    </reference-descriptor>
>>  </class-descriptor>
>>
>>
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> 
> 
> 
> 



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

Reply via email to