You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Gonçalo Luiz <go...@gmail.com> on 2006/01/16 02:05:03 UTC
Bidirectional 1:1 relations
Hello,
I've been trying this for hours without minimal success... I have a
A<->B relation the simpliest thing one can imagine. Each class has a
foreign key pointing to each other (a has bKey and b has aKey).
The problem is that when I check the database only one of the
instances have its foreign key value, the other one is zero.
I'm attaching my repository file and my business class that is
persisting the objects. I don't really understand why only one side of
the relation have the correct value.
In fact, in the attached files, I have some more occurencies of this
odd problem. However in 1:n bidirectional relationships its seems to
be no problem at all (maybe because there is only one foreign
key....).
I think this might be related to the fact that the value of a foreign
key is known only when the pointed instance is persisted... but how
can I fix this behaviour ?
And yes I'm using
auto-retrieve="true"
auto-update="none"
auto-delete="none"
as recomended.
Please let me know if yoy have any ideas.
Best regards,
--
Gonçalo Luiz
Re: Bidirectional 1:1 relations
Posted by Gonçalo Luiz <go...@gmail.com>.
Hello again,
Kindly forget my last e-mail I was looking to a old page of OJB that
was cached somewere... I think that the
http://db.apache.org/ojb/docu/guides/advanced-technique.html#nested-objects
page wil serve my needs... if it doesn't I will let you know.
Regards,
Gonçalo Luiz
On 1/20/06, Gonçalo Luiz <go...@gmail.com> wrote:
> Armin thank you for your reply,
>
> I came to a solution proposed to another OJB mailing poster (Luis
> Cruz) that works with me and suggested that I could invoke the
> getUniqueValue() in the DomainObject super class... its ugly but it
> works fine... and it happens to be a "controlled ugliness".
>
> I was now wondering... is it possible to map an object to two distinct
> columns of the same table? I mean if I have
>
> class Point
> {
> int x;
> int y;
> }
>
> and
> B
> {
> Point p;
> String name;
> }
>
> Can I use the B table to store the points? I was thinking about a
> table with the following schema:
> create table B (name varchar, point_x int, point_y int);
>
> I read the rowreader tutorial but it doesn't seem to fit my needs.
>
> Thank you once again!
>
> Best regards,
> Gonçalo Luiz
>
--
Gonçalo Luiz
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Bidirectional 1:1 relations
Posted by Gonçalo Luiz <go...@gmail.com>.
Armin thank you for your reply,
I came to a solution proposed to another OJB mailing poster (Luis
Cruz) that works with me and suggested that I could invoke the
getUniqueValue() in the DomainObject super class... its ugly but it
works fine... and it happens to be a "controlled ugliness".
I was now wondering... is it possible to map an object to two distinct
columns of the same table? I mean if I have
class Point
{
int x;
int y;
}
and
B
{
Point p;
String name;
}
Can I use the B table to store the points? I was thinking about a
table with the following schema:
create table B (name varchar, point_x int, point_y int);
I read the rowreader tutorial but it doesn't seem to fit my needs.
Thank you once again!
Best regards,
Gonçalo Luiz
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Bidirectional 1:1 relations
Posted by Armin Waibel <ar...@apache.org>.
Hi Gonçalo,
Gonçalo Luiz wrote:
> Hello,
> I've been trying this for hours without minimal success... I have a
> A<->B relation the simpliest thing one can imagine. Each class has a
> foreign key pointing to each other (a has bKey and b has aKey).
> The problem is that when I check the database only one of the
> instances have its foreign key value, the other one is zero.
>
> I'm attaching my repository file and my business class that is
> persisting the objects.
The business class was not attached (only the metadata file). Could you
post the source code again.
> I don't really understand why only one side of
> the relation have the correct value.
> In fact, in the attached files, I have some more occurencies of this
> odd problem. However in 1:n bidirectional relationships its seems to
> be no problem at all (maybe because there is only one foreign
> key....).
>
>
> I think this might be related to the fact that the value of a foreign
> key is known only when the pointed instance is persisted... but how
> can I fix this behaviour ?
>
Which version of OJB do you use?
> And yes I'm using
> auto-retrieve="true"
> auto-update="none"
> auto-delete="none"
> as recomended.
So I assume you are using the ODMG-api - correct?
It's a bidirectional 1:1 relation, thus if A was written to database the
PK of B is still 'null', thus A get 'null' as FK to B (or vice versa).
You can use OJB's ODMG-Extensions to solve your problem. Here is an
example using class Shop and ShopDetail with bidirectional 1:1 reference:
CircularTest.Shop s1 = new CircularTest.Shop(name + "_1");
CircularTest.ShopDetail sd = new CircularTest.ShopDetail(name + "_1");
s1.setDetail(sd);
sd.setShop(s1);
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
database.makePersistent(sd);
tx.flush();
database.makePersistent(s1);
tx.commit();
tx.begin();
database.deletePersistent(s1);
tx.flush();
database.deletePersistent(sd);
tx.commit();
regards,
Armin
>
> Please let me know if yoy have any ideas.
>
> Best regards,
> --
> Gonçalo Luiz
>
>
> ------------------------------------------------------------------------
>
> <class-descriptor class="org.goncalo.smsbridge.domain.SmsBridge"
> table="SMS_BRIDGE">
>
> <field-descriptor
> name="oid"
> column="OID"
> jdbc-type="INTEGER"
> primarykey="true"
>
> autoincrement="true"
> />
> <collection-descriptor
>
> auto-retrieve="true"
> auto-update="none"
> auto-delete="none"
> name="users"
> element-class-ref="org.goncalo.smsbridge.domain.User">
>
> <inverse-foreignkey field-ref="keyApplication"/>
> </collection-descriptor>
> </class-descriptor>
>
>
> <class-descriptor class="org.goncalo.smsbridge.domain.User"
> table="USER">
>
> <field-descriptor
> name="oid"
> column="OID"
> jdbc-type="INTEGER"
> primarykey="true"
>
> autoincrement="true"
> />
>
> <field-descriptor
> name="keyApplication"
> column="KEY_APPLICATION"
> jdbc-type="INTEGER"
>
> />
>
> <field-descriptor
> name="keyPerson"
> column="KEY_PERSON"
> jdbc-type="INTEGER"
>
> />
>
> <field-descriptor
> name="username"
> column="USERNAME"
> jdbc-type="VARCHAR"
> />
>
> <field-descriptor
> name="password"
> column="PASSWORD"
> jdbc-type="VARCHAR"
>
> />
>
> <field-descriptor
> name="timesLoggedin"
> column="TIMES_LOGGED_IN"
> jdbc-type="INTEGER"
>
> />
>
> <reference-descriptor
> auto-retrieve="true"
> auto-update="none"
> auto-delete="none"
> name="application"
> class-ref="org.goncalo.smsbridge.domain.SmsBridge">
> <foreignkey field-ref="keyApplication"/>
> </reference-descriptor>
>
> <reference-descriptor
> auto-retrieve="true"
> auto-update="none"
> auto-delete="none"
> name="person"
> class-ref="org.goncalo.smsbridge.domain.Person">
> <foreignkey field-ref="keyPerson"/>
> </reference-descriptor>
>
> </class-descriptor>
>
> <class-descriptor class="org.goncalo.smsbridge.domain.Person"
> table="PERSON">
>
> <field-descriptor
> name="oid"
> column="OID"
> jdbc-type="INTEGER"
> primarykey="true"
>
> autoincrement="true"
> />
>
> <field-descriptor
> name="keyAddressBook"
> column="KEY_ADDRESS_BOOK"
> jdbc-type="INTEGER"
>
> />
>
> <field-descriptor
> name="keyUser"
> column="KEY_USER"
> jdbc-type="INTEGER"
>
> />
>
> <field-descriptor
> name="name"
> column="NAME"
> jdbc-type="VARCHAR"
>
> />
>
> <reference-descriptor
> auto-retrieve="true"
> auto-update="none"
> auto-delete="none"
> name="addressBook"
> class-ref="org.goncalo.smsbridge.domain.addressBook.AddressBook">
>
> <foreignkey field-ref="keyAddressBook"/>
> </reference-descriptor>
>
> <reference-descriptor
> auto-retrieve="true"
> auto-update="none"
> auto-delete="none"
> name="user"
> class-ref="org.goncalo.smsbridge.domain.User">
>
> <foreignkey field-ref="keyUser"/>
> </reference-descriptor>
>
> </class-descriptor>
>
> <class-descriptor class="org.goncalo.smsbridge.domain.addressBook.AddressBook"
> table="ADDRESS_BOOK">
>
> <field-descriptor
> name="oid"
> column="OID"
> jdbc-type="INTEGER"
> primarykey="true"
>
> autoincrement="true"
> />
>
> <field-descriptor
> name="keyOwner"
> column="KEY_PERSON"
> jdbc-type="INTEGER"
>
> />
>
> <reference-descriptor
> auto-retrieve="true"
> auto-update="none"
> auto-delete="none"
> name="owner"
> class-ref="org.goncalo.smsbridge.domain.Person">
>
> <foreignkey field-ref="keyOwner"/>
> </reference-descriptor>
>
> </class-descriptor>
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org