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 ss...@intermediate.de on 2004/04/07 15:12:09 UTC

Empty Reference FK overwrites primary key ?

Hi,

I' ve got a simple object with mandant, id as primary key. As such I can insert it into the database.
If I now define a relation to another object using a foreign key my insert fails with primary key missing.
This is due to the fact that the Relation Object is null, i.e. the relation is not defined for this object.

I do not understand why ojb "overwrites" my primary key (mandant) from the missing relation ? I am using ojb rc5 and SAPDB.

Thanks for any ideas,

Stefan

E.g.

create table Adresse(
  mandant VARCHAR(3) not null,
  id FIXED(32) not null,
   plz VARCHAR(200),
 fk_person FIXED(32),
 primary key (mandant, id)
);
create table Person(
  mandant VARCHAR(3) not null,
  id FIXED(32) not null,
   name VARCHAR(200),
 primary key (mandant, id)
);

<class-descriptor    class="test.AdresseValue" table="Adresse">
    <field-descriptor  name="mandant" column="mandant" jdbc-type="VARCHAR" primarykey="true"/>
    <field-descriptor  name="id" column="ID" jdbc-type="BIGINT" primarykey="true"/>
    <field-descriptor  name="plz" column="plz" jdbc-type="VARCHAR"/>

    <field-descriptor  name="fk_person" column="fk_person" jdbc-type="BIGINT"  />

       <reference-descriptor  name="personObj"  class-ref="test.PersonValue"
            auto-retrieve="false" auto-update="false" auto-delete="false">
        <foreignkey field-ref="mandant"/>
        <foreignkey field-ref="fk_person"/>
    </reference-descriptor>
   </class-descriptor>

The sql generated looks like this:
INSERT INTO Adresse (mandant,ID,plz,fk_person) VALUES ('','1','761','0')




---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


1-1 Relationship with single FK ??

Posted by Stefan Schlösser <ss...@intermediate.de>.
Hi,

I would like to model a one-to-one Relationship between 2 classes.

For simplicity I would like to have a single FK in one of the classes 
only. Unfortunately OJB does not allow the definition of an 
"inverse-foreign-key" within the reference descriptor.

How do I solve this ? I don't wish to have a List in one of the classes, 
  I prefer the type-safe using a single Object.

If I had 2 fk's what happens upon update on one of the objects ? Which 
setting do I need to take to ensure consistency of both fks? I.e. update 
only the other classes fk but leave other fields untouched?

Thanks for any help on this, by the way I am using CVS head.

Cheers
   S.Schlösser


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Empty Reference FK overwrites primary key ?

Posted by Armin Waibel <ar...@apache.org>.
Hi,

sschloesser@intermediate.de wrote:
> Hi,
> 
> I' ve got a simple object with mandant, id as primary key. As such I can insert it into the database.
> If I now define a relation to another object using a foreign key my insert fails with primary key missing.
> This is due to the fact that the Relation Object is null, i.e. the relation is not defined for this object.
> 
> I do not understand why ojb "overwrites" my primary key (mandant) from the missing relation ? I am using ojb rc5 and SAPDB.
> 

Try latest from CVS (is stable). Fields defined as PK and part of a FK 
will never be set to 'null' when the reference is removed (reference is 
null).
Please read the updated documentation about 1:1 reference and "Linking"
http://db.apache.org/ojb/tutorial3.html#1:1%20auto-xxx%20setting

In rc5:
If you only set the FK but not the referenced object itself, OJB assume 
the reference is removed and set all FK to null (this is not expected 
when auto-update false is set, that's the reason why we improve the 
auto-xxx settings in rc6).
Try to set the FK, then call PB.retrieveAllReferences(...) (or 
PB.retrieveReference) to retrieve the referenced object and then update 
Address.

regards,
Armin

> Thanks for any ideas,
> 
> Stefan
> 
> E.g.
> 
> create table Adresse(
>   mandant VARCHAR(3) not null,
>   id FIXED(32) not null,
>    plz VARCHAR(200),
>  fk_person FIXED(32),
>  primary key (mandant, id)
> );
> create table Person(
>   mandant VARCHAR(3) not null,
>   id FIXED(32) not null,
>    name VARCHAR(200),
>  primary key (mandant, id)
> );
> 
> <class-descriptor    class="test.AdresseValue" table="Adresse">
>     <field-descriptor  name="mandant" column="mandant" jdbc-type="VARCHAR" primarykey="true"/>
>     <field-descriptor  name="id" column="ID" jdbc-type="BIGINT" primarykey="true"/>
>     <field-descriptor  name="plz" column="plz" jdbc-type="VARCHAR"/>
> 
>     <field-descriptor  name="fk_person" column="fk_person" jdbc-type="BIGINT"  />
> 
>        <reference-descriptor  name="personObj"  class-ref="test.PersonValue"
>             auto-retrieve="false" auto-update="false" auto-delete="false">
>         <foreignkey field-ref="mandant"/>
>         <foreignkey field-ref="fk_person"/>
>     </reference-descriptor>
>    </class-descriptor>
> 
> The sql generated looks like this:
> INSERT INTO Adresse (mandant,ID,plz,fk_person) VALUES ('','1','761','0')
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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