You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by "Ninck Blok, Jeroen" <je...@logica.com> on 2008/05/15 14:40:00 UTC
Circular Relationships Mapping
Hi All,
I am relative new to iBatis and want to use it to map a circular relationship. However I am experiencing some problems doing this.
I have two entities in Java: Set and Scenario. A Set can have zero or more scenarios and a set can have zero or more sets. I have created the following structure in the database: A table sets (with primary key set_id), a table scenarios (with primary key scenario_id) and a linking table scenario_sets (with a foreign key to scenario_id and twice to set_id: one for the parent set and for the child set).
I use the following mapping structure:
<resultMap id="set" class="Set">
<result property="setId" column="set_id"/>
<result property="name" column="name"/>
<result property="scenarios" column="set_id" select="getChildScenariosForSet"/>
<result property="sets" column="set_id" select="getChildSetsForSet"/>
</resultMap>
<resultMap id="scenario-simple" class="Scenario">
<result property="scenarioId" column="scenario_id"/>
<result property="name" column="name"/>
<result property="count" column="count"/>
<result property="displayId" column="display_id"/>
<result property="description" column="description"/>
</resultMap>
With the following select queries:
<select id="getScenario" parameterClass="Scenario" resultMap="scenario">
select *
from scenarios
where scenario_id = #scenarioId#
</select>
<select id="getAllSets" resultMap="set">
select s.*
from sets s
where not exists (
select 1
from scenario_sets ss
where ss.set_id = s.set_id
)
</select>
<select id="getChildScenariosForSet" parameterClass="int" resultMap="scenario-simple">
select s.*
from scenarios s,
scenario_sets ss
where ss.parent_set_id = #value#
and s.scenario_id = ss.scenario_id
</select>
<select id="getChildSetsForSet" parameterClass="int" resultMap="set">
select s.*
from sets s,
scenario_sets ss
where ss.parent_set_id = #value#
and s.set_id = ss.set_id
</select>
The Set class has got two lists. One for Set instances and one for Scenario instances.
When I fill the database like this:
- Set 1
o Set 2
§ Set 3
§ Set 4
o Set 5
- Set 6
Now when I call getAllSets it does not fully populate the child sets. Set 4 is not added to the list of Set 2, though I would expect this to happen. On page 39 of the developer guide I found a warning concerning circular relationships, however I did not find a (fitting) solution for more problem, since the number of child sets (and the level of child sets) is unlimited.
Is it possible to do the above with iBatis and I am doing it correctly?
Thanks!
Jeroen Ninck Blok
This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.