Orm 如何解决iBATIS中的(双向)父/子关系芯片
假设我有两个简单的表:Orm 如何解决iBATIS中的(双向)父/子关系芯片,orm,ibatis,mybatis,Orm,Ibatis,Mybatis,假设我有两个简单的表: Article: id | text Comment: id | articleId | text 两个DTO类: class Article { private int id; private String text; private List<Comment> comments; ... } class Comment { private int id; private String text;
Article: id | text
Comment: id | articleId | text
两个DTO类:
class Article {
private int id;
private String text;
private List<Comment> comments;
...
}
class Comment {
private int id;
private String text;
private Article article;
...
}
我想通过id选择一篇文章及其所有评论。关键的要求是,从查询返回的项目实例必须与其所有注释getArticle中的项目实例相同
有可能绘制出这张地图吗?我能想到的映射:
<resultMap id="ArticleResult" class="Article">
<result column="id" property="id" />
<result column="text" property="text" />
<result column="id" property="comments" select="findCommentsByArticleId" />
</resultMap>
<resultMap id="CommentResult" class="Comment">
<result column="id" property="id" />
<result column="text" property="text" />
<result column="articleId" property="article" select="findArticleById" />
</resultMap>
<select id="findArticleById" resultMap="ArticleResult" parameterClass="int">
SELECT * FROM Article WHERE id = #value#
</select>
<select id="findCommentsByArticleId" resultMap="CommentResult" parameterClass="int">
SELECT * FROM Comment WHERE articleId = #value#
</select>
但首先,在我看来这是一个循环,其次,我上面提到的关键要求将无法满足。你怎么看?请投票: 我相信关键是在resultMap中,应该使用而不是
双向关系很难维持。你的情况一定是这样吗?我的意思是,如果您可以删除文章中的列表注释,并用数据库查询替换对象导航,那么就容易多了。我现在正在DAO层中进行此操作,但我想知道iBATIS是否支持此操作。
<resultMap id="ParentResult" groupBy="id">
<result property="id" column="ID" />
...
<collection property="children" javaType="ArrayList" ofType="Child" resultMap="ChildResult" />
</resultMap>
<resultMap id="ChildResult">
<result property="id" column="ID" />
<association property="parentId" foreignColumn="PARENT_ID" resultMap="ParentResult" />
...
</result>