On Sun, 20 Feb 2005 15:06:17 +0100, Emir Causevic wrote:

>
>
>Hi,
>Here is a few questions regarding handling of 1:m and N:M relationships in
>Castor.
>Let's say I have three classes: Provider, Item, Part.
>Relationships are these:
>* one-to-many between Item and part Part
>* many-to-many between Item and Provider
>
>1. First question is regarding Castor requirement for bi-directional
>associations.
>These means that Item  class need to have collection of Parts and collection
>of Providers, as well as the Provider need to have collection of Items and
>Part need to have a reference to Item.
>Is this correct?
Yes.

>2. Second, if I understand things correctly, when I load list of all Items -
>the whole database will be loaded. 
Yes, unless of course you are using lazy loading.

>If I want to avoid loading all Parts and Providers together with Items I
>should use lazy loading.
Yes.

>Let's say that in my mapping for Item I have marked both Providers and Parts
>lazy loaded.
>Second question - what happens if I load list of Items, don't access any of
>the Parts (to avoid their loading ) and then db.commit().
>What will the Item.getParts() collection contain after commit?
Same as before. Lazy loading simply implies that the instance(s) identified by 
the relation as specified in thje field mapping will not be materialized until 
first access.

>3. Third and most important to me is on proper way of adding Parts (because
>they depend of Items due to 1:M).
>Can I add part like this:
>
>       Item i = db.load(...);
>       Part p = new Part(....);
>       p.setItem(i);
>       db.create(p);
>       db.commit()
>
>What is the proper way of adding many-side objects?
Practically, it should not make a differnce whether you set the Item (paren) on 
the part or the other way around. You could try ...

Item i = db.load(...);
Part p = new Part(....);
db.create(p);
i.addPart (p);
db.commit()

Please note that the db.create() should be before the i.add() in case you are 
using a key generator.

>4. Will Castor "complain" if I don't specify relationships between classes
>in mapping file? Could that cause some problems to Castor?
Yes, it could. At least 1:M and M:N relations need to be bi-directional.

>I assume that in this case, the foreign-key field in the Parts won't be
>reference to Item, but of type the Item key is (int in my case).
>Is this correct?
>   
>
>Regards,
>Emir
>
>
>
>----------------------------------------------------------- 
>If you wish to unsubscribe from this mailing, send mail to
>[EMAIL PROTECTED] with a subject of:
>        unsubscribe castor-user
>



----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-user

Reply via email to