I am trying to write a MyBatis Mapper for the following java objects, and the output of the mapper expects List as return type.
class Blog{
private final String name;
private final String author;
private final List<Posts> posts;
// I follow builder pattern, so the below private constructor is intended for MyBatis alone
private Blog(String name, String author, List<Posts> posts){
this.name = name;
this.author= author;
this.posts= posts;
...... excluding getters and builder class as it it not relevant in this case
}
The post object would look like below
class Post{
private final String postName;
private final String postSummary;
// Same here as well. Private constructor only to satisfy MyBatis.
private Post(String postName, String postSummary) {
this.postName = postname;
this.postSummary = postSummary;
}
The BlogRepoMapper is as below:
public List<Blog> getAllBlogPosts();
BlogMapper.xml
<select id="getAllBlogPosts" resultMap = "blogResultMapper"
BLOGNAME, AUTHOR, POSTNAME, POSTSUMMARY from BLOGDETAILS>
</select>
<resultMap id="blogResultMapper" type="Blog">
<constructor>
<arg column="BLOGNAME" javaType="String" />
<arg column="AUTHOR" javaType="String" />
</constructor>
<collection property="posts" ofType="posts" resultMap="postsMapper" />
</resultMap>
<resultMap id="postsMapper" type="Posts">
<constructor>
<arg column="POSTNAME" javaType="String" />
<arg column="POSTSUMMARY" javaType="String" />
</constructor>
</resultMap>
I'm not sure how to write the resultmap for the select query as it involves collection of Post objects.I tried a couple of times but got the below exception
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class Blog with invalid types (String,String) or values (JavaBlog, JavaAuthor). Cause: java.lang.NoSuchMethodException: Blog.<init>(java.lang.String, java.lang.String)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) ~[mybatis-spring-1.3.1.jar:1.3.1]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) ~[mybatis-spring-1.3.1.jar:1.3.1]
at com.sun.proxy.$Proxy78.selectList(Unknown Source) ~[na:na]
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) ~[mybatis-spring-1.3.1.jar:1.3.1]
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137) ~[mybatis-3.4.3.jar:3.4.3]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75) ~[mybatis-3.4.3.jar:3.4.3]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) ~[mybatis-3.4.3.jar:3.4.3]
Highly appreciate any help to get a solution for the above mapper.