OJB (as well as Hibernate, others) tries to behave as an Object Database... As such, it shines when dealing with objects as opposed to ids (which are artificial entities anyway, created for the purposes of a database lookup, they don't exist in real life). Think about it in this way if you need to create a brand new LocaleBean made up of a brand new CountryBean and a brand new LanguageBean, you don't have id for any of these three objects. However, though OJB "object database" the ids are irrelevant as long as you associate the right objects together. Behind the scenes, OJB will assign the right ids to the right objects when persisting to your tables.
When using OJB et.al. think objects, don't think ids... If you think ids, OJB will try to insert NULL all over the place as you found out. A good strategy I follow is to cache objects like Language or Country lists (which don't change that often, if ever). So when I create a brand new Whatever and i need a Country to associate it with, I already have the Country in memory and I don't have to search for it. Axel -----Original Message----- From: Zhe Liu [mailto:[EMAIL PROTECTED] Sent: Thursday, November 20, 2003 2:42 AM To: [EMAIL PROTECTED] Subject: How to set value of foreign keys Hi, I have a table core_locale, which has two foreign keys. Following is the class descriptor. I also created a corresponding value object LocaleBean, which includes field properties: countryCode and languageCode as well as reference properties: countryCodeCountry and languageCodeLanguage. All getter/setter methods are also created. However, I found that when I try to save an instance of LocaleBean, if I only set values for countryCode and languageCode (without setting values for countryCodeCountry and languageCodeLanguage), it will try to insert NULL into COUNTRY_CODE and LANGUAGE_CODE columns. But it will work if I set valules for countryCodeCountry and languageCodeLanguage, while leave countryCode and languageCode as null. Looks like the values for countryCodeCountry and languageCodeLanguage will always overwrite the values for countryCode and languageCode. I am just wondering whether this is purposely designed in this way and if so, what's the reason. When auto-update is set to false (which is default), I think it makes more sense for user to just set the values for foreighn keys instead of the whole reference objects. Thanks, Zhe <!-- ============================================================== TABLE CORE_LOCALE ============================================================== --> <class-descriptor class="com.gsnx.prototype.core.server.util.lang.LocaleBean" table="CORE_LOCALE" > <!-- COLUMN LOCALE_CODE .............................................. --> <field-descriptor id="1" name="localeCode" column="LOCALE_CODE" jdbc-type="VARCHAR" nullable="false" primarykey="true" /> <!-- COLUMN LANGUAGE_CODE .............................................. --> <field-descriptor id="3" name="languageCode" column="LANGUAGE_CODE" jdbc-type="VARCHAR" nullable="false" /> <!-- COLUMN COUNTRY_CODE .............................................. --> <field-descriptor id="4" name="countryCode" column="COUNTRY_CODE" jdbc-type="VARCHAR" nullable="false" /> <reference-descriptor name="countryCodeCountry" class-ref="com.gsnx.prototype.core.server.util.country.CountryBean"> <foreignkey field-ref="countryCode"/> </reference-descriptor> <reference-descriptor name="languageCodeLanguage" class-ref="com.gsnx.prototype.core.server.util.lang.LanguageBean"> <foreignkey field-ref="languageCode"/> </reference-descriptor> </class-descriptor> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
