RE: Can not recognize the object passed in for the toOne relationship?

From: Bill Fan (bil..anscom.com.au)
Date: Fri Apr 21 2006 - 03:06:23 EDT

  • Next message: Lindsay Steele: "Re: PK problem with Postgre"

    Thanks Andrus!

    I've just figured out the problem... It has nothing to do with Cayenne. I
    messed up with where the price object should be created with Spring.

    The problem was that I created the Price object list in the
    UpdatePriceAction class within Struts by using
            Price price = new Price();
            Price.setPrice(o.getPrice());
            ...

            list.add(price);

    , without setting up the product target by using
    price.setToProduct(product).

    Thought it was just purely java bean to hold the information, no need to
    worry about the data relationship yet. Then I passed the price object list
    to the save(list, product) function in PriceDaoImpl class and tried to do
    new Price() again as indicated in my previous message, plus setting up the
    product target, then commitChanges...

    Now, I made some changes to only create the Price object in the PriceDaoImpl
    class, and it is working now.

    Cheers,
    Bill

    -----Original Message-----
    From: Andrus Adamchik [mailto:andru..bjectstyle.org]
    Sent: Friday, 21 April 2006 4:36 PM
    To: cayenne-use..ncubator.apache.org
    Subject: Re: Can not recognize the object passed in for the toOne
    relationship?

    Hi Bill,

    Many-to-one relationships usually work without surprises. Can you
    check a few more things right before commit:

    Log.debug("prices: " + product.getPriceArray());
    Log.debug("product: " + price.getToProduct());

    Andrus

    On Apr 21, 2006, at 8:41 AM, Bill Fan wrote:

    > Hi,
    >
    > I'm trying to do something like the following and it seems the
    > productId
    > becomes null in the generated sql query.
    >
    > Price price = new Price();
    > threadDataContext().registerNewObject(price);
    > price.setToProduct(product);
    > price.setPrice(o.getPrice());
    > ...
    >
    > The query generated was as following:
    > ------------------------------------
    > INSERT INTO dbo.Price (Description, GST, IsUnitPrice, Price, PriceId,
    > PriceTypeId, ProductId, Qty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    > [bind: '1 box', 10, 'true', 100, 280, 1, NULL, 1]
    >
    >
    > I'm using Tomcat 5.0.28 + Spring 1.2.7 + Cayenne 1.1.4 + Struts 1.2.9
    >
    > I can see and verify the productId value and the product name by
    > placing the
    > following logging statements before the product object is used,
    > Log.debug("product Id: " + product.getProductId());
    > Log.debug("product Name: " + product.getProductName());
    >
    >
    >
    >
    > More details are as follow if it is required:
    > =============================================
    > In the web app, there are a Product table and a Price table. It is
    > a "one to
    > many" relation from Product to Price,
    >
    > The tables:
    > -----------
    > Product table:
    > proudctId int PK
    > productName varchar(100)
    > ...
    >
    > Price table:
    > priceId int PK,
    > productId int FK (to Product.ProductId column)
    > price double
    > ...
    >
    > Cayenne data map:
    > -----------------
    > <db-relationship name="toProduct" source="Price" target="Product"
    > toMany="false">
    > <db-attribute-pair source="ProductId" target="ProductId"/>
    > </db-relationship>
    >
    > <db-relationship name="priceArray" source="Product" target="Price"
    > toMany="true">
    > <db-attribute-pair source="ProductId" target="ProductId"/>
    > </db-relationship>
    >
    >
    > Many thanks in advance!
    > Bill



    This archive was generated by hypermail 2.0.0 : Fri Apr 21 2006 - 03:07:00 EDT