ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Poitras Christian" <Christian.Poit...@ircm.qc.ca>
Subject RE: Polymorphism question
Date Fri, 23 Mar 2007 21:22:10 GMT
You can always do some left joins with the id. Something like.
Select id, col1, col2, type, colX, colY, colZ, colW
>From BaseTable LEFT JOIN ChildTable1 ON BaseTable.id = ChildTable1.id
LEFT JOIN ChildTable2 ON BaseTable.id = ChildTable2.id
Then any resultMap for these will work since the columns must be there
if the map reads it, otherwise columns are unnecessary.
This approch seems to be the only thing that would work with a join.
Still, you can do a twist with discriminator tag.
<resultMap id="baseMap">
  <discriminator column="type">
    <subMap value="1" resultMap="child1">
    <subMap value="2" resultMap="child2">
<resultMap id="map1">
  <result property="subClass" property="id" select="getChildInfo1"/>
<resultMap id="map2">
  <result property="subClass" property="id" select="getChildInfo2"/>
By making sure the subClass calls setters of your main class, you can
force a second select to be call to populate values from child table.
I doudt you can make a reference to the populating object itself (being
able to write "this" would be a cool improvement!) inside the
disciminator when you would like a second call to database...
Possible improvement.
<resultMap id="map1">
  <result property="this" property="id" select="getChildInfo1"/>


From: Mikael Andersson [mailto:mail.micke@gmail.com] 
Sent: Friday, 23 March 2007 14:49
To: user-java@ibatis.apache.org
Subject: Polymorphism question

I have a polymorphism problem which I'm not sure is solvable by a pure
iBATIS solution.

I have a fixed database model which represents a hierarchy(very

| id |col1 | col2 | type | 

| id |colX | colY |

| id |colZ | colW |

Which has an obvious object hierarchy, and "type" contains a either 'A'
or 'B' indicating which child table should be used.

The id of the base and the child is the same, and the same id can not
exists in the two child tables. 

I have found information which shows how the <discriminator> element can
be used with a column indicating the object to create. Which solves half
of my problem.

The other half is something which I don't think is solvable, but figured
I'd ask just in case it is. 
I need the select join statement to be modified depending on the value
of the "type" column, which table to join with. I have no idea how to do
this in iBATIS, and am currently leaning towards implementing this in
Java logic (first get the type from the base table and then execute the
correct select statement). 

Any help appreciated,

View raw message