First of all I'd like to apologize for having asked a question which
has obviously been asked before.
I tried all the things described in the message below but I still have the
same problem.
I do not want to create a new Product with creating an Order
(which implicitly creates OrderItems which worked fine from the beginning).
I just want to store a reference to the Product in OrderItem (At least
that's what
I tried to express with my mapping file).
When I use bi-direction nothing changes at all, productid is
still NULL.
When I use setAutoCommit(true) a new Product is created for
each stored Order (which is not the desired behaviour).
The only thing which helped so far is to remove the key-generator attribute
from the mapping of class Product. Then it works fine for this part of
my application. Of course, then creating Products in another part of the
application does not work unless I generate the key for a Product by myself.
Another solution I found is to implement the Persistent interface in
OrderItem
and add the code
product_ = (Product) db.load(Product.class, product_.getId());
to jdoBeforeCreate().
But I still do not understand why I either have to remove the key-generator
or have to reload the Product object. Currently I think these solutions are
workarounds which I need because I did something wrong.
If anyone could help me further to find out what I did wrong or explain why
it is working only with the solutions described above, I would be very
thankful.
Kind regards,
Lothar
-----Urspr�ngliche Nachricht-----
Von: Thomas Yip [mailto:[EMAIL PROTECTED]]
Gesendet: Donnerstag, 31. Januar 2002 20:39
An: [EMAIL PROTECTED]
Betreff: Re: [castor-dev] NULL value is inserted instead of key value
I think it question has been risen many times. But, each user use
description differently and nobody ever think they're the same problem.
Normally, the NULL is inserted, either because your relationship is
uni-direction. And, uni-direction is not supported. In other words,
make a back reference is a must.
On the other hand, you need to synchronize the bi-direction yourself.
Meaning that you must do it in your code:
class foo {
void setBar( Bar bar ) {
if ( bar != null )
bar.setFoo( this );
}
}
The third reason is for non-dependent object.
a) you didn't setAutoStore( true ),
b) and you didn't explicitly create the second object,
before explicitly the first one.
In other word, you either do (a) or (b), but not both.
Thomas
(PS. I will try break the update(...) hack if I know why it works,
but I still don't understand why it would.)
-----Original Message-----
>From: Ricardo Arg�ello [mailto:[EMAIL PROTECTED]]
>Sent: Thursday, January 31, 2002 10:35 AM
>To: [EMAIL PROTECTED]
>Subject: Re: [castor-dev] NULL value is inserted instead of key value
>
>Hello,
>
>Would somebody write an authoritative answer on what is the CORRECT
solution?
>
>It looks like there is no concense on what is the "propper way" to use
Castor.
>
>Somebody comes up with a "hack" to make it act the intended way, another
one has yet nother "hack", but there is not a "propper way" in the docs, or
in the website, or in the CVS!
>
>Castor looks like Perl: "There is more than one way to do it". That is Not
Good (TM).
>
>Thanks.
>
>
>----- Original Message -----
>From: "Peter T. Brown" <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Sent: Thursday, January 31, 2002 1:14 PM
>Subject: Re: [castor-dev] NULL value is inserted instead of key value
>
>
>> I have had this same problem.. I could never find a 'correct' solution,
so
>> have resorted to calling update() on the related collection. So in your
>> example below I would add update(item) before committing the transaction.
>> This seems to force castor to work properly.
>>
>> Peter
>>
>> -----Original Message-----
>> From: Bernecker Lothar [mailto:[EMAIL PROTECTED]]
>> Sent: Thursday, January 31, 2002 1:08 AM
>> To: [EMAIL PROTECTED]
>> Subject: [castor-dev] NULL value is inserted instead of key value
>>
>>
>> Hi,
>>
>> I have the problem that a NULL is entered into my table instead of the
value
>> of the foreign key.
>> I have the following mapping specified:
>>
>> <mapping>
>> <class name="cat.Product" identity="id" key-generator="Unique">
>> <description>Product definition</description>
>> <map-to table="product" />
>> <field name="id" type="string">
>> <sql name="id" type="varchar" />
>> </field>
>> <field name="name" type="string">
>> <sql name="name" type="char" />
>> </field>
>> </class>
>>
>> <class name="cat.Order" identity="id" key-generator="Unique">
>> <description>An order</description>
>> <map-to table="orders" />
>> <field name="id" type="string">
>> <sql name="id" type="varchar" />
>> </field>
>> <field name="orderDate" type="date">
>> <sql name="orderdate" type="date" />
>> </field>
>> <field name="orderItems" type="castortest.OrderItem"
required="true"
>> collection="collection">
>> <sql many-key="orderid"/>
>> </field>
>> </class>
>>
>> <class name="cat.OrderItem" identity="id" depends="castortest.Order"
>> key-generator="Unique">
>> <description>An order</description>
>> <map-to table="orderitem" />
>> <field name="id" type="string">
>> <sql name="id" type="varchar" />
>> </field>
>> <field name="product" type="castortest.Product">
>> <sql name="productid" />
>> </field>
>> <field name="quantity" type="integer">
>> <sql name="quantity" type="integer" />
>> </field>
>> <field name="order" type="castortest.Order">
>> <sql name="orderid" />
>> </field>
>> </class>
>>
>> <key-generator name="UUID" alias="Unique"/>
>>
>> </mapping>
>>
>> When I use the following sequence to create a new order then the
productid
>> column in order item
>> is always NULL:
>>
>> Product p = null;
>> .
>> .
>> db = jdo.getDatabase();
>>
>> db.begin();
>>
>> query = db.getOQLQuery(oql);
>> qr = query.execute();
>>
>> if (qr.hasMore()) {
>> p = (Product) qr.next();
>> }
>>
>> db.commit();
>>
>> db.begin();
>>
>> Order order = new Order();
>> OrderItem item = new OrderItem();
>> item.setOrder(order);
>> item.setProduct(p);
>> item.setQuantity(1);
>> order.addOrderItem(item);
>>
>> db.create(order);
>>
>> db.commit();
>> db.close();
>>
>> The strange this is that this only happens when I use a key-generator for
>> Product. When I remove
>> the key-generator attribute then it works fine in this context. Can
anyone
>> tell me what I did wrong?
>>
>> Kind regards,
>>
>> Lothar
>>
>> -----------------------------------------------------------
>> If you wish to unsubscribe from this mailing, send mail to
>> [EMAIL PROTECTED] with a subject of:
>> unsubscribe castor-dev
>>
>> -----------------------------------------------------------
>> If you wish to unsubscribe from this mailing, send mail to
>> [EMAIL PROTECTED] with a subject of:
>> unsubscribe castor-dev
>>
>>
>
>-----------------------------------------------------------
>If you wish to unsubscribe from this mailing, send mail to
>[EMAIL PROTECTED] with a subject of:
> unsubscribe castor-dev
>
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev