ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Farsi, Reza" <Reza.Fa...@SUNGARD.DE>
Subject Getting of composed objects
Date Tue, 06 Sep 2005 09:35:05 GMT
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)

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:
	<resultMap id="instrument" class="instrument">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="dividendComponent" select="getInstrumentDevidend" column="id"/>
	<resultMap id="dvidend" class="dividend">
		<result property="internalNumber" column="internal_number"/>

	<select id="getInstrumentDevidend" resultMap="instrumentDividend" parameterClass="int">
		SELECT * FROM instrument_dividend WHERE instrument_id=#value#
	   | 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 id="saveInstrumentDividend" parameterClass="instrument">
		INSERT INTO instrument_dividend (instrument_id, internal_number) VALUES (#id#, #dividendComponent.internalNumber#);

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().insert("saveInstrument", instrument);
			if (instrument.getDividendComponent() != null) {
				getSqlMapClient().insert("saveInstrumentDividend", instrument);
		} catch (SQLException ex) {
			throw new RuntimeException(ex);
		} finally {
			try {
			} 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?


View raw message