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 ABOU LINA <am...@gmail.com> on 2006/02/13 10:38:36 UTC

URGENT please, : MAPPING BIDIRECTIONAL NAVIGATION 1:1 ??? ojb 1.0.3

Hi,

i give you this sample example :

in case  A<---->B  [1:n] :
     the mapping of this bidiractional navigation is guaranted by the use of
inverse-foreignkey :

     Mapping A classe :
     ----------------------------------------------------

    <collection-descriptor
       name="allB"
       element-class-ref="B"
    >
       <inverse-foreignkey field-ref="aId"/>
    </collection-descriptor>


    Mapping B class :
    ------------------------------
      <reference-descriptor
         name="a"
         class-ref="A"
      >
         <foreignkey field-ref="aId"/>

      </reference-descriptor>

so in case of  A<---->B  [1:1] i need a bidirectional navigation !!!!!
how to use ????

my config :
  1. ojb 1.0.3
  2. i use the broker (PB-API)



Thanks a lot ....

Re: URGENT please, : MAPPING BIDIRECTIONAL NAVIGATION 1:1 ??? ojb 1.0.3

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

ABOU LINA wrote:
> Thanks Armin, your reply is very helpfull.
> so there is no direct solution we must call store two time , one for A
> another for B.
> in your opinion why is not possible to use the same philosophy of the
> invers-foreignekey wich is used in 1:n ????

Assume you are using database identity based PK's. In that case the PK 
value was assigned after the object was insert, e.g. A. Then OJB was 
able to assign the FK in B and insert B. But the FK to B in A wasn't 
set, thus OJB have to assign the FK in A and update A.

If you manage the PK values by your own and the PK values are valid 
before insert of objects, it's possible to establish the bidirectional 
objects with a single store call.


> 
> an other question about performance this time :
> 
> i don't know how can we do it in OJB ; when OJB prepare a statement (for
> read) by default he make SELECT * FROM ... so i think if we have
> a posibility to specify only fields that's we need instead of (*) we got a
> good result at performance level in reading. ??? how can i do this in OJB
> 

If only need a few fields of an persistent object class and not the 
whole persistent object itself, it's recommended to use ReportQueries:
http://db.apache.org/ojb/docu/guides/query.html#Report+Queries

Much more complex will be the use of different metadata profiles 
(different persistent object metadata mapping for the same objects), 
e.g. a normal profile and specific read profiles (without specific fields).
http://db.apache.org/ojb/docu/guides/metadata.html#Per+thread+metadata+changes

regards,
Armin


> thx in advance.
> 
> 
> On 2/14/06, Armin Waibel <ar...@apache.org> wrote:
>> Hi,
>>
>> ABOU LINA wrote:
>>> Hi,
>>>
>>> i give you this sample example :
>>>
>>> in case  A<---->B  [1:n] :
>>>      the mapping of this bidiractional navigation is guaranted by the
>> use of
>>> inverse-foreignkey :
>>>
>>>      Mapping A classe :
>>>      ----------------------------------------------------
>>>
>>>     <collection-descriptor
>>>        name="allB"
>>>        element-class-ref="B"
>>>     >
>>>        <inverse-foreignkey field-ref="aId"/>
>>>     </collection-descriptor>
>>>
>>>
>>>     Mapping B class :
>>>     ------------------------------
>>>       <reference-descriptor
>>>          name="a"
>>>          class-ref="A"
>>>       >
>>>          <foreignkey field-ref="aId"/>
>>>
>>>       </reference-descriptor>
>>>
>>> so in case of  A<---->B  [1:1] i need a bidirectional navigation !!!!!
>>> how to use ????
>>>
>> Do you mean how to map it?
>> Declare in both classes A and B a 1:1 reference-descriptor (both class
>> tables need a FK column).
>>
>> How to insert?
>> This depends strongly on the used auto-xxx settings. If
>> auto-update/delete is enabled (and PK values handled by OJB) you can do
>> e.g.:
>>
>> broker.beginTransaction();
>> // first store both objects
>> broker.store(a, ObjectModification.INSERT);
>> // or let OJB detect the needed operation (more overhead)
>> //broker.store(a);
>> broker.store(b, ObjectModification.INSERT);
>> // now set references
>> a.setRelatedB(b);
>> b.setRelatedA(a);
>> // update both
>> broker.store(a, ObjectModification.UPDATE);
>> broker.commitTransaction();
>>
>> or ()
>>
>> // set references first
>> a.setRelatedB(b);
>> b.setRelatedA(a);
>> broker.beginTransaction();
>> // store both objects
>> broker.store(a);
>> // update b to force OJB to set the FK
>> // back to a (was 'null' after first store call)
>> broker.store(b);
>> broker.commitTransaction();
>>
>> or if auto-update 'link' is used
>>
>> broker.beginTransaction();
>> // first store both objects
>> broker.store(a, ObjectModification.INSERT);
>> broker.store(b, ObjectModification.INSERT);
>> // now set references
>> a.setRelatedB(b);
>> b.setRelatedA(a);
>> // update both
>> broker.store(a, ObjectModification.UPDATE);
>> broker.store(b, ObjectModification.UPDATE);
>> broker.commitTransaction();
>> ...
>>
>> regards,
>> Armin
>>
>>
>>> my config :
>>>   1. ojb 1.0.3
>>>   2. i use the broker (PB-API)
>>>
>>>
>>>
>>> Thanks a lot ....
>>>
>>
>> ---------------------------------------------------------------------
>> 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


Re: URGENT please, : MAPPING BIDIRECTIONAL NAVIGATION 1:1 ??? ojb 1.0.3

Posted by ABOU LINA <am...@gmail.com>.
Thanks Armin, your reply is very helpfull.
so there is no direct solution we must call store two time , one for A
another for B.
in your opinion why is not possible to use the same philosophy of the
invers-foreignekey wich is used in 1:n ????

an other question about performance this time :

i don't know how can we do it in OJB ; when OJB prepare a statement (for
read) by default he make SELECT * FROM ... so i think if we have
a posibility to specify only fields that's we need instead of (*) we got a
good result at performance level in reading. ??? how can i do this in OJB

thx in advance.


On 2/14/06, Armin Waibel <ar...@apache.org> wrote:
>
> Hi,
>
> ABOU LINA wrote:
> > Hi,
> >
> > i give you this sample example :
> >
> > in case  A<---->B  [1:n] :
> >      the mapping of this bidiractional navigation is guaranted by the
> use of
> > inverse-foreignkey :
> >
> >      Mapping A classe :
> >      ----------------------------------------------------
> >
> >     <collection-descriptor
> >        name="allB"
> >        element-class-ref="B"
> >     >
> >        <inverse-foreignkey field-ref="aId"/>
> >     </collection-descriptor>
> >
> >
> >     Mapping B class :
> >     ------------------------------
> >       <reference-descriptor
> >          name="a"
> >          class-ref="A"
> >       >
> >          <foreignkey field-ref="aId"/>
> >
> >       </reference-descriptor>
> >
> > so in case of  A<---->B  [1:1] i need a bidirectional navigation !!!!!
> > how to use ????
> >
>
> Do you mean how to map it?
> Declare in both classes A and B a 1:1 reference-descriptor (both class
> tables need a FK column).
>
> How to insert?
> This depends strongly on the used auto-xxx settings. If
> auto-update/delete is enabled (and PK values handled by OJB) you can do
> e.g.:
>
> broker.beginTransaction();
> // first store both objects
> broker.store(a, ObjectModification.INSERT);
> // or let OJB detect the needed operation (more overhead)
> //broker.store(a);
> broker.store(b, ObjectModification.INSERT);
> // now set references
> a.setRelatedB(b);
> b.setRelatedA(a);
> // update both
> broker.store(a, ObjectModification.UPDATE);
> broker.commitTransaction();
>
> or ()
>
> // set references first
> a.setRelatedB(b);
> b.setRelatedA(a);
> broker.beginTransaction();
> // store both objects
> broker.store(a);
> // update b to force OJB to set the FK
> // back to a (was 'null' after first store call)
> broker.store(b);
> broker.commitTransaction();
>
> or if auto-update 'link' is used
>
> broker.beginTransaction();
> // first store both objects
> broker.store(a, ObjectModification.INSERT);
> broker.store(b, ObjectModification.INSERT);
> // now set references
> a.setRelatedB(b);
> b.setRelatedA(a);
> // update both
> broker.store(a, ObjectModification.UPDATE);
> broker.store(b, ObjectModification.UPDATE);
> broker.commitTransaction();
> ...
>
> regards,
> Armin
>
>
> > my config :
> >   1. ojb 1.0.3
> >   2. i use the broker (PB-API)
> >
> >
> >
> > Thanks a lot ....
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>

Re: URGENT please, : MAPPING BIDIRECTIONAL NAVIGATION 1:1 ??? ojb 1.0.3

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

ABOU LINA wrote:
> Hi,
> 
> i give you this sample example :
> 
> in case  A<---->B  [1:n] :
>      the mapping of this bidiractional navigation is guaranted by the use of
> inverse-foreignkey :
> 
>      Mapping A classe :
>      ----------------------------------------------------
> 
>     <collection-descriptor
>        name="allB"
>        element-class-ref="B"
>     >
>        <inverse-foreignkey field-ref="aId"/>
>     </collection-descriptor>
> 
> 
>     Mapping B class :
>     ------------------------------
>       <reference-descriptor
>          name="a"
>          class-ref="A"
>       >
>          <foreignkey field-ref="aId"/>
> 
>       </reference-descriptor>
> 
> so in case of  A<---->B  [1:1] i need a bidirectional navigation !!!!!
> how to use ????
>

Do you mean how to map it?
Declare in both classes A and B a 1:1 reference-descriptor (both class 
tables need a FK column).

How to insert?
This depends strongly on the used auto-xxx settings. If 
auto-update/delete is enabled (and PK values handled by OJB) you can do 
e.g.:

broker.beginTransaction();
// first store both objects
broker.store(a, ObjectModification.INSERT);
// or let OJB detect the needed operation (more overhead)
//broker.store(a);
broker.store(b, ObjectModification.INSERT);
// now set references
a.setRelatedB(b);
b.setRelatedA(a);
// update both
broker.store(a, ObjectModification.UPDATE);
broker.commitTransaction();

or ()

// set references first
a.setRelatedB(b);
b.setRelatedA(a);
broker.beginTransaction();
// store both objects
broker.store(a);
// update b to force OJB to set the FK
// back to a (was 'null' after first store call)
broker.store(b);
broker.commitTransaction();

or if auto-update 'link' is used

broker.beginTransaction();
// first store both objects
broker.store(a, ObjectModification.INSERT);
broker.store(b, ObjectModification.INSERT);
// now set references
a.setRelatedB(b);
b.setRelatedA(a);
// update both
broker.store(a, ObjectModification.UPDATE);
broker.store(b, ObjectModification.UPDATE);
broker.commitTransaction();
...

regards,
Armin


> my config :
>   1. ojb 1.0.3
>   2. i use the broker (PB-API)
> 
> 
> 
> Thanks a lot ....
> 


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