Skip to content

Commit 772d200

Browse files
committed
Fixes #168. For consistency with XML the property should be called
fetchType() and not lazy().
1 parent 6b60097 commit 772d200

File tree

5 files changed

+73
-23
lines changed

5 files changed

+73
-23
lines changed

src/main/java/org/apache/ibatis/annotations/Many.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
public @interface Many {
3131
String select() default "";
3232

33-
FetchType lazy() default FetchType.DEFAULT;
33+
FetchType fetchType() default FetchType.DEFAULT;
3434

3535
}

src/main/java/org/apache/ibatis/annotations/One.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
public @interface One {
3131
String select() default "";
3232

33-
FetchType lazy() default FetchType.DEFAULT;
33+
FetchType fetchType() default FetchType.DEFAULT;
3434

3535
}

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,12 @@ private String nestedSelectId(Result result) {
500500

501501
private boolean isLazy(Result result) {
502502
Boolean isLazy = null;
503-
if (FetchType.DEFAULT != result.one().lazy()) {
504-
isLazy = (result.one().lazy() == FetchType.LAZY);
503+
if (FetchType.DEFAULT != result.one().fetchType()) {
504+
isLazy = (result.one().fetchType() == FetchType.LAZY);
505505
}
506-
if (FetchType.DEFAULT != result.many().lazy()) {
506+
if (FetchType.DEFAULT != result.many().fetchType()) {
507507
if (isLazy == null) {
508-
isLazy = (result.many().lazy() == FetchType.LAZY);
508+
isLazy = (result.many().fetchType() == FetchType.LAZY);
509509
} else {
510510
throw new BuilderException("Cannot use both @One and @Many annotations in the same @Result");
511511
}

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
import javax.sql.DataSource;
3333

34+
import net.sf.cglib.proxy.Factory;
35+
3436
import org.apache.ibatis.BaseDataTest;
3537
import org.apache.ibatis.executor.result.DefaultResultHandler;
3638
import org.apache.ibatis.mapping.Environment;
@@ -189,20 +191,6 @@ public void shouldExecuteBoundSelectListOfBlogsStatement() {
189191
session.close();
190192
}
191193
}
192-
193-
@Test
194-
public void shouldGetBlogsWithAuthors() {
195-
SqlSession session = sqlSessionFactory.openSession();
196-
try {
197-
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
198-
List<Blog> blogs = mapper.selectBlogsWithAutors();
199-
assertEquals(2, blogs.size());
200-
assertEquals(101, blogs.get(0).getAuthor().getId());
201-
assertEquals(102, blogs.get(1).getAuthor().getId());
202-
} finally {
203-
session.close();
204-
}
205-
}
206194

207195
@Test
208196
public void shouldExecuteBoundSelectMapOfBlogsById() {
@@ -706,4 +694,44 @@ public void shouldCacheMapperMethod() throws Exception {
706694
}
707695
}
708696

697+
@Test
698+
public void shouldGetBlogsWithAuthorsAndPosts() {
699+
SqlSession session = sqlSessionFactory.openSession();
700+
try {
701+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
702+
List<Blog> blogs = mapper.selectBlogsWithAutorAndPosts();
703+
assertEquals(2, blogs.size());
704+
assertTrue(blogs.get(0) instanceof Factory);
705+
assertEquals(101, blogs.get(0).getAuthor().getId());
706+
assertEquals(1, blogs.get(0).getPosts().size());
707+
assertEquals(1, blogs.get(0).getPosts().get(0).getId());
708+
assertTrue(blogs.get(1) instanceof Factory);
709+
assertEquals(102, blogs.get(1).getAuthor().getId());
710+
assertEquals(1, blogs.get(1).getPosts().size());
711+
assertEquals(2, blogs.get(1).getPosts().get(0).getId());
712+
} finally {
713+
session.close();
714+
}
715+
}
716+
717+
@Test
718+
public void shouldGetBlogsWithAuthorsAndPostsEagerly() {
719+
SqlSession session = sqlSessionFactory.openSession();
720+
try {
721+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
722+
List<Blog> blogs = mapper.selectBlogsWithAutorAndPostsEagerly();
723+
assertEquals(2, blogs.size());
724+
assertFalse(blogs.get(0) instanceof Factory);
725+
assertEquals(101, blogs.get(0).getAuthor().getId());
726+
assertEquals(1, blogs.get(0).getPosts().size());
727+
assertEquals(1, blogs.get(0).getPosts().get(0).getId());
728+
assertFalse(blogs.get(1) instanceof Factory);
729+
assertEquals(102, blogs.get(1).getAuthor().getId());
730+
assertEquals(1, blogs.get(1).getPosts().size());
731+
assertEquals(2, blogs.get(1).getPosts().get(0).getId());
732+
} finally {
733+
session.close();
734+
}
735+
}
736+
709737
}

src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import domain.blog.Blog;
2020
import domain.blog.DraftPost;
2121
import domain.blog.Post;
22+
2223
import org.apache.ibatis.annotations.*;
24+
import org.apache.ibatis.mapping.FetchType;
2325
import org.apache.ibatis.session.RowBounds;
2426

2527
import java.util.List;
@@ -139,6 +141,12 @@ List<Post> selectPostsLikeSubjectAndBody(RowBounds bounds,
139141

140142
//======================================================
141143

144+
@Select("SELECT * FROM " +
145+
"post WHERE id = #{id}")
146+
List<Post> selectPostsById(int id);
147+
148+
//======================================================
149+
142150
@Select("SELECT * FROM blog " +
143151
"WHERE id = #{id} AND title = #{nonExistentParam,jdbcType=VARCHAR}")
144152
Blog selectBlogByNonExistentParam(@Param("id") int id);
@@ -166,12 +174,26 @@ List<Post> selectPostsLikeSubjectAndBody(RowBounds bounds,
166174
"WHERE ${column} = #{id} AND title = #{value}")
167175
Blog selectBlogWithAParamNamedValue(@Param("column") String column, @Param("id") int id, @Param("value") String title);
168176

177+
//======================================================
178+
169179
@Select({
170180
"SELECT *",
171181
"FROM blog"
172182
})
173-
@Results({ @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor")) })
174-
List<Blog> selectBlogsWithAutors();
183+
@Results({
184+
@Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor")),
185+
@Result(property = "posts", column = "id", many = @Many(select = "selectPostsById"))
186+
})
187+
List<Blog> selectBlogsWithAutorAndPosts();
175188

176-
189+
@Select({
190+
"SELECT *",
191+
"FROM blog"
192+
})
193+
@Results({
194+
@Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor", fetchType=FetchType.EAGER)),
195+
@Result(property = "posts", column = "id", many = @Many(select = "selectPostsById", fetchType=FetchType.EAGER))
196+
})
197+
List<Blog> selectBlogsWithAutorAndPostsEagerly();
198+
177199
}

0 commit comments

Comments
 (0)