Skip to content

Commit f8d01ee

Browse files
committed
Added another test case for #215. The association element in the authorResult does not have columnPrefix attribute, so MyBatis tries to lookup ancestor stored under the same resultMapID.
1 parent 9873e39 commit f8d01ee

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

src/test/java/org/apache/ibatis/submitted/ancestor_ref/AncestorRefTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,20 @@ public void testCircularCollection() {
7373
sqlSession.close();
7474
}
7575
}
76+
77+
@Test
78+
public void testAncestorRef() {
79+
SqlSession sqlSession = sqlSessionFactory.openSession();
80+
try {
81+
Mapper mapper = sqlSession.getMapper(Mapper.class);
82+
Blog blog = mapper.selectBlog(1);
83+
assertEquals("Author1", blog.getAuthor().getName());
84+
assertEquals("Author2", blog.getCoAuthor().getName());
85+
// author and coauthor should have a ref to blog
86+
assertEquals(blog, blog.getAuthor().getBlog());
87+
assertEquals(blog, blog.getCoAuthor().getBlog());
88+
} finally {
89+
sqlSession.close();
90+
}
91+
}
7692
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.submitted.ancestor_ref;
18+
19+
public class Author {
20+
private Integer id;
21+
private String name;
22+
private Blog blog;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public Blog getBlog() {
41+
return blog;
42+
}
43+
44+
public void setBlog(Blog blog) {
45+
this.blog = blog;
46+
}
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.submitted.ancestor_ref;
18+
19+
public class Blog {
20+
private Integer id;
21+
private String title;
22+
private Author author;
23+
private Author coAuthor;
24+
25+
public Integer getId() {
26+
return id;
27+
}
28+
29+
public void setId(Integer id) {
30+
this.id = id;
31+
}
32+
33+
public String getTitle() {
34+
return title;
35+
}
36+
37+
public void setTitle(String title) {
38+
this.title = title;
39+
}
40+
41+
public Author getAuthor() {
42+
return author;
43+
}
44+
45+
public void setAuthor(Author author) {
46+
this.author = author;
47+
}
48+
49+
public Author getCoAuthor() {
50+
return coAuthor;
51+
}
52+
53+
public void setCoAuthor(Author coAuthor) {
54+
this.coAuthor = coAuthor;
55+
}
56+
}

src/test/java/org/apache/ibatis/submitted/ancestor_ref/CreateDB.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,24 @@ insert into users (id, name) values
3232

3333
insert into friend (user_id, friend_id) values
3434
(1, 2), (2, 2), (2, 3);
35+
36+
drop table blog if exists;
37+
drop table author if exists;
38+
39+
create table blog (
40+
id int,
41+
title varchar(16),
42+
author_id int,
43+
co_author_id int
44+
);
45+
46+
create table author (
47+
id int,
48+
name varchar(16)
49+
);
50+
51+
insert into blog (id, title, author_id, co_author_id) values
52+
(1, 'Blog1', 1, 2), (2, 'Blog2', 2, 3);
53+
54+
insert into author (id, name) values
55+
(1, 'Author1'), (2, 'Author2'), (3, 'Author3');

src/test/java/org/apache/ibatis/submitted/ancestor_ref/Mapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public interface Mapper {
1919

2020
User getUserAssociation(Integer id);
2121
User getUserCollection(Integer id);
22+
Blog selectBlog(Integer id);
2223

2324
}

src/test/java/org/apache/ibatis/submitted/ancestor_ref/Mapper.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,30 @@
5555
order by uf.id
5656
</select>
5757

58+
<resultMap type="org.apache.ibatis.submitted.ancestor_ref.Blog"
59+
id="blogResult">
60+
<id property="id" column="id" />
61+
<result property="title" column="title" />
62+
<association property="author" resultMap="authorResult"
63+
columnPrefix="author_" />
64+
<association property="coAuthor" resultMap="authorResult"
65+
columnPrefix="co_author_" />
66+
</resultMap>
67+
68+
<resultMap type="org.apache.ibatis.submitted.ancestor_ref.Author"
69+
id="authorResult">
70+
<id property="id" column="id" />
71+
<result property="name" column="name" />
72+
<association property="blog" resultMap="blogResult" />
73+
</resultMap>
74+
75+
<select id="selectBlog" resultMap="blogResult"><![CDATA[
76+
select id, title, a.id author_id, a.name author_name,
77+
ca.id co_author_id, ca.name co_author_name
78+
from blog b
79+
left join author a on a.id = b.author_id
80+
left join author ca on ca.id = b.co_author_id
81+
where b.id = #{id}
82+
]]></select>
83+
5884
</mapper>

0 commit comments

Comments
 (0)