ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mikael Andersson" <mail.mi...@gmail.com>
Subject Re: GroupBy + Discriminator + Polymorphism
Date Fri, 13 Apr 2007 10:43:18 GMT
Perhaps something like this (wild guess work :) ) :

     <resultMap class="Container" id="root" groupBy="id">
               <result property="id" column="id_container"/>
               <result property="arrayListOfSuper" resultMap="polyMap"/>
       </resultMap>

       <resultMap id="polyMap" extends="superMap">
                <discriminator javaType="String" column="entity_type">
                       <subMap value="foo" resultMap="fooMap"/>
                       <subMap value="bar" resultMap="barMap"/>
               </discriminator>
       </resultMap>

       <resultMap id="superMap" class="Super">
              <result property="id" column="id"/>
       </resultMap>

       <resultMap id="fooMap" class="Foo" extends="superMap">
               <result property="name" column="name">
       </resultMap>
       <resultMap id="barMap" class="Bar" extends="superMap">
               <result property="name" column="name">
       </resultMap>


- Mike

On 13/04/07, Daniel Carleton <daniel.carleton@pobox.com> wrote:
>
> Hello,
>
> I'm attempting to map an ArrayList<SuperClass> typed property and
> populate it with instances of SubClassFoo and SubClassBar from the
> database.
>
> In my query I LEFT JOIN, and then check for NULL primary key values to
> determine which type of SubClass each row represents.  Then I use a
> <discriminator> and <subMaps> to delegate each row type to the
> appropriate mapping.  I also groupBy my containing object's ID to get
> all SubClassFoo and SubClassBar objects into the ArrayList<SuperClass>
> together.
>
> Only problem is, it's not working as expected.  iBATIS returns an
> ArrayList containing two instances of my containing class.  One instance
> contains all the SubClassFoo instances, and the other the SubClassBar
> instances.
>
> Anyone know how I can achieve a heterogeneous list?  I'm trying to
> leverage the sorting capabilities of the database, otherwise I'd just
> use two SELECTs, one for each type.
>
> Simplified code below, where Foo and Bar are subclasses of Super.
>
> <sqlMap namespace="Example">
>         <resultMap class="Container" id="root" groupBy="id">
>                 <result property="id" column="id"/>
>                 <discriminator javaType="String" column="entity_type">
>                         <subMap value="foo" resultMap="fooSub"/>
>                         <subMap value="bar" resultMap="barSub"/>
>                 </discriminator>
>         </resultMap>
>         <resultMap id="fooSub" class="Container" extends="root">
>                 <result property="arrayListOfSuper" resultMap="fooMap"/>
>         </resultMap>
>         <resultMap id="barSub" class="Container" extends="root">
>                 <result property="arrayListOfSuper" resultMap="barMap"/>
>         </resultMap>
>         <resultMap id="fooMap" class="Foo">
>                 <result property="id" column="id">
>                 <result property="name" column="name">
>         </resultMap>
>         <resultMap id="barMap" class="Bar">
>                 <result property="id" column="id">
>                 <result property="name" column="name">
>         </resultMap>
>         <statement id="getContainer" resultMap="root">
>                 SELECT containers.id_container,
>                         foos.id, foos.name, bars.id, bars.name,
>                         CASE
>                         WHEN foos.id IS NULL THEN 'foo'
>                         WHEN bars.id IS NULL THEN 'bar'
>                         END AS entity_type
>
>                 FROM containers
>                         LEFT JOIN foos USING(id_container)
>                         LEFT JOIN bars USING(id_container)
>                 WHERE containers.id_container = 1
>                         AND ( foos.id IS NOT NULL OR bars.id IS NOT NULL)
>         </statement>
> </sqlMap>
>
> Thoughts?
>
> Thanks,
>
> - Daniel
>
>
>

Mime
View raw message