I managed to resolve my confusion. In this case, a join isn't
appropriate. You'll want to use a select, so instead of using this

<result property="Articles" column="Id"
resultMapping="Article.article"/>

You want something like this

<result property="Articles" column="Id"
select="getArticleByCategoryId"/> 

The exception that's thrown when a column is absent from a result set
could certainly use some work.

-----Original Message-----
From: Shawn Hinsey [mailto:[EMAIL PROTECTED] 
Sent: Thursday, March 30, 2006 10:29 AM
To: [email protected]
Subject: Complex property problem

Hello,

I'm having what's probably a really dumb problem with complex property
mappings. I think I may be misunderstanding part of the documentation.

I'd like to implement a complex property using a join, in order to avoid
the n+1 select problem.

Say I have three tables. Article, Category, and CategoryArticles, which
maps them in an many to many relationship. 

Here're what I think the mapping files should look like. 

<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="Category" xmlns="http://ibatis.apache.org/mapping";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; >
        <resultMaps>
                <resultMap class="Category" id="category-select">
                        <result property="Id" column="Id"/>
                        <result property="Name" column="Name"/>
                        <result property="SortOrder"
column="SortOrder"/>
                        <result property="Articles" column="Id"
resultMapping="Article.article"/>
                </resultMap>
        </resultMaps>

        <statements>
                <select id="selectAllCategories"
resultMap="category-select">
                        SELECT * FROM Category
                </select>

                <select id="selectCategoryById"
resultMap="category-select">
                        SELECT * FROM Category WHERE Id = #id#
                </select>

        </statements>
</sqlMap>

And

<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="Article" xmlns="http://ibatis.apache.org/mapping";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; >
        <resultMaps>
                <resultMap class="Article" id="article">
                        <result property="Id" column="Id"/>
                        <result property="Description"
column="Description"/>
                        <result property="Location" column="Location"/>
                        <result property="HarvestDate"
column="HarvestDate"/>
                        <result property="SortOrder"
column="SortOrder"/>
                </resultMap>
        </resultMaps>

        <statements>
                <select id="selectAllArticles" resultMap="article">
                        SELECT * FROM Article
                </select>

                <select id="selectArticleById"
extends="selectAllArticles" resultMap="article">
                        WHERE Id = #value#
                </select>
        </statements>
</sqlMap>

Executing selectAllCategories or selectCategoryById causes this
exception to be thrown: 

[IndexOutOfRangeException: Location]
   System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
+66
   System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) +104
 
IBatisNet.DataMapper.TypeHandlers.StringTypeHandler.GetValueByName(Resul
tProperty mapping, IDataReader dataReader) +18

What's going on that I'm missing?

Thanks,
Shawn

Shawn Hinsey | Senior Software Developer | MIVA | [EMAIL PROTECTED]
Tel: (617) 768-5220, ext. 239 | Fax: (617) 768-5221 

MIVA Means Business
www.miva.com | NASDAQ:MIVA

Reply via email to