Skip to content

Commit

Permalink
Fixes mybatis#168. For consistency with XML the property should be ca…
Browse files Browse the repository at this point in the history
…lled

fetchType() and not lazy().
  • Loading branch information
emacarron committed Mar 30, 2014
1 parent 6b60097 commit 772d200
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/apache/ibatis/annotations/Many.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
public @interface Many {
String select() default "";

FetchType lazy() default FetchType.DEFAULT;
FetchType fetchType() default FetchType.DEFAULT;

}
2 changes: 1 addition & 1 deletion src/main/java/org/apache/ibatis/annotations/One.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
public @interface One {
String select() default "";

FetchType lazy() default FetchType.DEFAULT;
FetchType fetchType() default FetchType.DEFAULT;

}
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ private String nestedSelectId(Result result) {

private boolean isLazy(Result result) {
Boolean isLazy = null;
if (FetchType.DEFAULT != result.one().lazy()) {
isLazy = (result.one().lazy() == FetchType.LAZY);
if (FetchType.DEFAULT != result.one().fetchType()) {
isLazy = (result.one().fetchType() == FetchType.LAZY);
}
if (FetchType.DEFAULT != result.many().lazy()) {
if (FetchType.DEFAULT != result.many().fetchType()) {
if (isLazy == null) {
isLazy = (result.many().lazy() == FetchType.LAZY);
isLazy = (result.many().fetchType() == FetchType.LAZY);
} else {
throw new BuilderException("Cannot use both @One and @Many annotations in the same @Result");
}
Expand Down
56 changes: 42 additions & 14 deletions src/test/java/org/apache/ibatis/binding/BindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import javax.sql.DataSource;

import net.sf.cglib.proxy.Factory;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.executor.result.DefaultResultHandler;
import org.apache.ibatis.mapping.Environment;
Expand Down Expand Up @@ -189,20 +191,6 @@ public void shouldExecuteBoundSelectListOfBlogsStatement() {
session.close();
}
}

@Test
public void shouldGetBlogsWithAuthors() {
SqlSession session = sqlSessionFactory.openSession();
try {
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
List<Blog> blogs = mapper.selectBlogsWithAutors();
assertEquals(2, blogs.size());
assertEquals(101, blogs.get(0).getAuthor().getId());
assertEquals(102, blogs.get(1).getAuthor().getId());
} finally {
session.close();
}
}

@Test
public void shouldExecuteBoundSelectMapOfBlogsById() {
Expand Down Expand Up @@ -706,4 +694,44 @@ public void shouldCacheMapperMethod() throws Exception {
}
}

@Test
public void shouldGetBlogsWithAuthorsAndPosts() {
SqlSession session = sqlSessionFactory.openSession();
try {
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
List<Blog> blogs = mapper.selectBlogsWithAutorAndPosts();
assertEquals(2, blogs.size());
assertTrue(blogs.get(0) instanceof Factory);
assertEquals(101, blogs.get(0).getAuthor().getId());
assertEquals(1, blogs.get(0).getPosts().size());
assertEquals(1, blogs.get(0).getPosts().get(0).getId());
assertTrue(blogs.get(1) instanceof Factory);
assertEquals(102, blogs.get(1).getAuthor().getId());
assertEquals(1, blogs.get(1).getPosts().size());
assertEquals(2, blogs.get(1).getPosts().get(0).getId());
} finally {
session.close();
}
}

@Test
public void shouldGetBlogsWithAuthorsAndPostsEagerly() {
SqlSession session = sqlSessionFactory.openSession();
try {
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
List<Blog> blogs = mapper.selectBlogsWithAutorAndPostsEagerly();
assertEquals(2, blogs.size());
assertFalse(blogs.get(0) instanceof Factory);
assertEquals(101, blogs.get(0).getAuthor().getId());
assertEquals(1, blogs.get(0).getPosts().size());
assertEquals(1, blogs.get(0).getPosts().get(0).getId());
assertFalse(blogs.get(1) instanceof Factory);
assertEquals(102, blogs.get(1).getAuthor().getId());
assertEquals(1, blogs.get(1).getPosts().size());
assertEquals(2, blogs.get(1).getPosts().get(0).getId());
} finally {
session.close();
}
}

}
28 changes: 25 additions & 3 deletions src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import domain.blog.Blog;
import domain.blog.DraftPost;
import domain.blog.Post;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.session.RowBounds;

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

//======================================================

@Select("SELECT * FROM " +
"post WHERE id = #{id}")
List<Post> selectPostsById(int id);

//======================================================

@Select("SELECT * FROM blog " +
"WHERE id = #{id} AND title = #{nonExistentParam,jdbcType=VARCHAR}")
Blog selectBlogByNonExistentParam(@Param("id") int id);
Expand Down Expand Up @@ -166,12 +174,26 @@ List<Post> selectPostsLikeSubjectAndBody(RowBounds bounds,
"WHERE ${column} = #{id} AND title = #{value}")
Blog selectBlogWithAParamNamedValue(@Param("column") String column, @Param("id") int id, @Param("value") String title);

//======================================================

@Select({
"SELECT *",
"FROM blog"
})
@Results({ @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor")) })
List<Blog> selectBlogsWithAutors();
@Results({
@Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor")),
@Result(property = "posts", column = "id", many = @Many(select = "selectPostsById"))
})
List<Blog> selectBlogsWithAutorAndPosts();


@Select({
"SELECT *",
"FROM blog"
})
@Results({
@Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor", fetchType=FetchType.EAGER)),
@Result(property = "posts", column = "id", many = @Many(select = "selectPostsById", fetchType=FetchType.EAGER))
})
List<Blog> selectBlogsWithAutorAndPostsEagerly();

}

0 comments on commit 772d200

Please sign in to comment.