title: How can I create bi-directional mappings with MyBatis 3.0.5?
body: <p>Say I have two domain objects and a mapper interface.</p>
  
  <pre class="lang-java prettyprint-override"><code>class Person {
      int id;
      List&lt;Problem&gt; problems = new ArrayList&lt;Problem&gt;();
  }
  
  class Problem {
      int id;
      Person person;
  }
  
  interface PersonMapper {
      public List&lt;Person&gt; selectAllPersons();
  }
  </code></pre>
  
  <p>And two database tables.</p>
  
  <pre class="lang-sql prettyprint-override"><code>create table person (
      id integer not null generated always as identity constraint person_pk primary key,
  )
  
  create table problem (
      id integer not null generated always as identity constraint problem_pk primary key,
      person_id integer not null constraint problem_person_fk references person
  )
  </code></pre>
  
  <p>I can create a mapping file that gets the data I want.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;resultMap id="personMap" type="Person"&gt;
      &lt;id column="person_id" property="id" /&gt;
      &lt;collection column="problem_person_id" property="problems"
                  javaType="ArrayList" ofType="Problem" resultMap="problemMap" /&gt;
  &lt;/resultMap&gt;
  
  &lt;resultMap id="problemMap" type="Problem"&gt;
      &lt;id column="problem_id" property="id" /&gt;
      &lt;!-- Adding an association here will cause a circular dependency --&gt;
      &lt;!-- The circular dependency results in a StackOverflowException --&gt;
  &lt;/resultMap&gt;
  
  &lt;select id="selectAllPersons" resultMap="personMap"&gt;
      select
          person.id as person_id,
          problem.id as problem_id
      from person left outer join problem on person.id = problem.person_id
  &lt;/select&gt;
  </code></pre>
  
  <p>However, since MyBatis doesn't do bi-directional mapping, none of the <code>Problem</code> objects in the returned collections will have their <code>Person</code> reference set correctly.</p>
  
  <p>According to <a href="http://code.google.com/p/mybatis/issues/detail?id=322" rel="nofollow">this issue</a>, it sounds like I should be able to update my mapper interface and add a custom result handler that can be supplied by the calling class.</p>
  
  <pre class="lang-java prettyprint-override"><code>interface PersonMapper {
      public List&lt;Person&gt; selectAllPersons(ResultHandler handler);
  }
  
  class PersonResultHandler implements ResultHandler {
      @Override
      public void handleResult(ResultContext context) {
          System.out.println(context.getResultObject());
      }
  }
  
  class PersonDAO {
      // Get SqlSession sqlSession
      sqlSession.getMapper(PersonMapper.class).selectAllPersons(new PersonResultHandler());
  }
  </code></pre>
  
  <p>However, the <code>handleResult</code> method of my <code>ResultHandler</code> never gets called.  I've seen <a href="http://code.google.com/p/mybatis/wiki/ResultHandlerExample" rel="nofollow">this example</a>, but the extra fluff class in there makes it very difficult to understand.  Can anyone give me a simple example of using a custom <code>ResultHandler</code> with mapper interfaces?  I'm using MyBatis 3.0.5+.</p>
  
  <p>I've also read through the MyBatis mailing list and there are several suggestions of using caching and lazy loading to solve circular dependencies, but I can't find any examples of how to do it.</p>