iBatis 1:N mapping 프레임워크2006. 11. 1. 09:53
게시판 시스템에 Article과 ArticleFile이라는 객체가 있고 이들이 1:N의 관계가 있다면 Article에는 아래와 같은 attribute이 존재할 것이다.
private Collection
이러한 경우 Article을 읽을 때 n개의 ArticleFile을 outer join을 이용하여 eager fetching하기 위한 맵핑은 아래와 같다.
<resultMap id="articleFilesResultMap" class="articleFile">
<result property="fhandle" column="fhandle"/>
<result property="articleId" column="articleFileArticleId"/>
<result property="fileName" column="fileName"/>
<result property="contentType" column="articleFileContentType"/>
<result property="size" column="articleFileSize"/>
<result property="regDate" column="articleFileRegDate"/>
</resultMap>
<resultMap id="latestArticleDetailMap" class="article" extends="articleResultMap" groupBy="id">
<result property="articleContent.id" column="contentId" />
<result property="articleContent.content" column="content" />
<result property="commentCount" column="commentCount"/>
<result property="articleFiles" resultMap="Article.articleFilesResultMap"/>
</resultMap>
<select id="getLatestArticles" parameterClass="map" resultMap="latestArticleDetailMap">
SELECT
a.*,
ac.id contentId, ac.content,
af.fhandle, af.articleId articleFileArticleId, af.fileName,
af.contentType articleFileContentType, af.size articleFileSize,
af.regDate articleFileRegDate,
(SELECT COUNT(c.id) FROM articleA000001comment c WHERE c.articleId = a.id) commentCount
FROM
articleA000001 a , articleA000001content ac
LEFT OUTER JOIN articleA000001file af ON af.articleId = a.id
WHERE
a.id = ac.id
AND
(status='S' OR status='L' OR deletedButDisplay=1)
ORDER BY
a.depth DESC
LIMIT 0, 100
</select>
여기서 latestArticleDetailMap의 groupBy="id"가 키포인트이다.