> I have a class
>
> public class Part{
> private Integer id;
> private String name;
> private Bar bar;
>
> ....
> getters/setters
> }
>
> I have a mapping for part and for there part I am adding aliases for column
> names after join.
>
> I am sure it is not correct way to do it. Please advice how to do correct
> way.
>
> Current mapping looks like
>
> <resultMap id="part"
> class="domain.Part">
> <result property="name" column="name" />
> <result property="id" column="id" />
> <result property="bar" resultMap="part.bar" />
> </resultMap>
>
>
> <resultMap id="otherPart"
> class="domain.Part">
> <result property="name" column="otherName" />
> <result property="id" column="otherId" />
> <result property="bar" resultMap="part.otherBar" />
> </resultMap>
>
> SQL
>
> select
> p1.id,
> p1.name,
> b.*,
> p1.id as otherId,
> p1.name as otherName
> from PART p1
> join BAR b on p1.id=b.p1Id
> join PART p2 on p2.id=b.p2Id
>
>
> Basically question is
> How to map the objects if during the querying the same table is join more
> then one time?
That depends on the object graph, you want to get. An entity
relationship diagram or class diagram would be helpful. Supposed you have
Bar 1 -- 2 Part
and
Bar
-part1 : Part
-part2 : Part
and want to get a list of bars, you can do the following
<resultMap id="bar.result" resultClass="domain.Bar">
<result property="part1" resultMap="part"/>
<result property="part2" resultMap="otherPart"/>
...
</resultMap>
<select id="bar.selectAll" resultMap="bar.result">
select
p1.id,
p1.name,
b.*,
p1.id as otherId,
p1.name as otherName
from PART p1
join BAR b on p1.id=b.p1Id
join PART p2 on p2.id=b.p2Id
</select>
List<Bar> list = getSqlMapClientTemplate().queryForList("bar.selectAll");
Ingmar