0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
technoJ
  • 175
  • 1
  • 1
  • 16
  • Please post the full exception stacktrace, and post the full `BlogRepoMapper`. – Mark Rotteveel Dec 02 '17 at 08:23
  • @MarkRotteveel wondering why you marked it duplicate.. my query was on constructing immutable objects (which holds list of objects) with mybatis mapper – technoJ Dec 05 '17 at 13:55
  • You're right, but your question wasn't very specific about this. In any case, you are defining a constructor `Blog(String, String, List)` and your mapper definition defines a constructor `Blog(String, String)`, so make sure your mapper definition matches the actual constructor. – Mark Rotteveel Dec 05 '17 at 16:33
  • Yeah.. I tried the above approach, but however MyBatis is failing to set the values for the List. When the object is constructed, the two string values are assigned proper values, but list is null – technoJ Dec 06 '17 at 04:07
  • You can use the setters to initialize the values instead of going thru the complicated way of constructors. Just remove the constructor and use `` much easier – MohamedSanaulla Dec 12 '17 at 05:58
  • 1
    Setter is not being used, as my domain class (Blog) is immutable. – technoJ Dec 12 '17 at 07:55

0 Answers0