Mapping one set of DB results into two java models of the same type...

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Mapping one set of DB results into two java models of the same type...

Marko Rihtar
Hi,

i'm using iBatis version 2.3.4.726.
Is it ok to post my question here under myBatis?

Marko
Reply | Threaded
Open this post in threaded view
|

Re: Mapping one set of DB results into two java models of the same type...

Marko Rihtar
Here is my problem with iBatis:

I have one class with 2 properties of the same type, i.e.

public class Stats{
    private Drive inboundDrive;
    private Drive outboundDrive;
}

This info is stored in DB in the same set of columns like this:

... driveType,    driveProp1,   driveProp2, ...
-----------------------------------------------------------------
... OUTGOING, property,      property, ...
... OUTGOING, property,      property, ...
... INCOMING,  property,      property, ...
... INCOMING,  property,      property, ...

I have to map same set of records to two model properties depending on
the driveType column value.
First I solved it using subselects, but this degrades preformance to
much.

Then i used DISCRIMINATOR tag like this:

        <resultMap id="statsResult" class="stats" groupBy="id">
                <result property="id" column="stats_id" />
                <result property="outboundDrive"
resultMap="Stats.outboundDriveResult" />
                <result property="inboundDrive"
resultMap="Stats.inboundDriveResult" />
        </resultMap>

        <resultMap id="outboundDriveResult" class="driveCombination">
                <discriminator javaType="string" column="driveType">
                 <subMap value="OUTBOUND"
resultMap="driveCombinationResult"/>
                </discriminator>
        </resultMap>

        <resultMap id="inboundDriveResult" class="driveCombination">
                <discriminator javaType="string" column="driveType">
                        <subMap value="INBOUND"
resultMap="driveCombinationResult"/>
                </discriminator>
        </resultMap>

        <resultMap id="driveCombinationResult" class="driveCombination"
groupBy="id">
                <result property="id" column="drive_combination_id" />
                ...
                <result property="flights" resultMap="Stats.flightResult" />
        </resultMap>

It works but i was wondering if there is a more appropriate way to
solve this kind of problem, because DISCRIMINATOR is used for solving
inheritance problems?

Thx for help.

Marko