Hi Nathan, I'm trying to optimize the code. For example, I wanna delegate the check, if referenced objects are NULL to the iBATIS.
Reza -----Ursprüngliche Nachricht----- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Gesendet: Dienstag, 6. September 2005 21:35 An: user-java@ibatis.apache.org Betreff: Re: Getting of composed objects I believe that you are doing everything correct. Are things not working or are you trying to find a better solution? Nathan On Sep 6, 2005, at 3:35 AM, Farsi, Reza wrote: > Hi all, > > I'm testing iBATIS by the means of one simple scenario using object > composition. > > There are two objects Instrument and Dividend. On the side of the > database the tables are defined as followed: > create table instrument ( > id int not null auto_increment primary key, > name varchar(30) not null > ); > > create table instrument_dividend ( > instrument_id int not null primary key, > internal_number int not null, > > constraint instrument_dividend_fk_01 foreign key > (instrument_id) references instrument (id) ON DELETE CASCADE ON > UPDATE CASCADE > ); > > instrument uses a auto generated key. A Dividend object uses as > primary key the id of the referencing Instrument. There is a 0..1 > relation between objects. > > The iBATIS files are defines as follows: > > <sqlMap> > <resultMap id="instrument" class="instrument"> > <result property="id" column="id"/> > <result property="name" column="name"/> > <result property="dividendComponent" > select="getInstrumentDevidend" column="id"/> > </resultMap> > > <resultMap id="dvidend" class="dividend"> > <result property="internalNumber" column="internal_number"/> > </resultMap> > > <select id="getInstrumentDevidend" > resultMap="instrumentDividend" parameterClass="int"> > SELECT * FROM instrument_dividend WHERE instrument_id=#value# > </select> > > <!-- > | DAO save/update query > --> > <insert id="saveInstrument" parameterClass="instrument"> > INSERT INTO instrument (name) VALUES (#name#) > <selectKey keyProperty="id" resultClass="int">SELECT > last_insert_id()</selectKey > > </insert> > > <insert id="saveInstrumentDividend" parameterClass="instrument"> > INSERT INTO instrument_dividend (instrument_id, > internal_number) VALUES (#id#, #dividendComponent.internalNumber#); > </insert> > </sqlMap> > > The problem is, that I can't save an Instrument by calling > saveInstrument. It doesn't save the referenced dividend. > > Here is the code for saving an Instrument: > /** > * saves the given instrument > */ > private void saveInstrument(Instrument instrument) { > > try { > getSqlMapClient().startTransaction(); > getSqlMapClient().insert("saveInstrument", instrument); > if (instrument.getDividendComponent() != null) { > getSqlMapClient().insert("saveInstrumentDividend", > instrument); > } > getSqlMapClient().commitTransaction(); > > } catch (SQLException ex) { > throw new RuntimeException(ex); > } finally { > try { > getSqlMapClient().endTransaction(); > } catch (SQLException ex) { > throw new RuntimeException(ex); > } > } > } > > At the moment the application has to check, if an Instrument object > references a Dividend obejct and if it is not null, the application > should save the referenced object explicitly ba calling > "saveInstrumentDividend". How can say iBATIS that it should save > the instrument and all referencing objects? > > Regards > Reza >