Search code examples
javaibatis

How do you handle sql mapping for multiple tables in ibatis and java


I am trying to use Ibatis with GWT and I have this scenario, I have databases tables Airport, Terminals and Flights. An airport can have different terminals. A terminal can have one Airport and many flights. And a flight can have one terminal. So the table structure looks like this.

Airport -id -name -terminal_id

Terminals -id -name -flight_id

Flights -id -airline -terminal_id

My select statement looks like this

SELECT airport.name AS Airport,
       terminals.name AS Terminal,
       flights.airline,
FROM airport,
     terminals,
     flights
WHERE airport.terminal_id = terminals.id
  AND terminals.flight_id = flights.id;

What will the sql maps look like to get this result. Where I'm getting confused is the result set is a combination of tables and so the result set isn't a model object of either of the three tables.


Solution

  • Create a custom value object(vo) to suit your need.

    <sqlMap namespace="Arrival">
    <resultMap id="Arrival" class="com.flight.vo.Arrival">
        <result property="airport" column="Airport" />
        <result property="terminal" column="Terminal" />
        <result property="airline" column="airline"/>
    </resultMap>
    
    <select id="retrieveAllArrivals" resultMap="Arrival.Arrival" >
        select airport.name as Airport, terminals.name as Terminal, flights.airline
        FROM airport, terminals, flights 
        WHERE airport.terminal_id = terminals.id 
        AND terminals.flight_id = flights.id
    </select>
    </sqlMap>