title: How to migrate join queries from Hibernate Criteria to MyBatis
body: <p>I'm migrating some queries from a module with Hibernate to another with MyBatis. The first module performs some queries using the Criteria API.</p>
  <p>My model is something like:</p>
  <pre><code>public class SomeEntity {
      private Long id;
      private String name;
      private AnotherEntity ae;
      ...
  }
  
  public class AnotherEntity {
      private Long id;
      ...
  }
  </code></pre>
  <p>Of course that this have a standard implementation in the DB with <code>SOME_ENTITY.AE_ID</code> with a FK and so on. With the Hibernate implementation you have something like:</p>
  <pre><code>public List&lt;SomeEntity&gt; getSomeEntity(String name, AnotherEntity ae) {
      DetachedCriteria criteria = DetachedCriteria.forClass(SomeEntity.class);
      criteria.add(Property.forName(&quot;name&quot;).eq(name));
      criteria.add(Property.forName(&quot;ae&quot;).eq(ae));
      return (List&lt;SomeEntity&gt;) hibernateTemplate.findByCriteria(criteria);
  }
  </code></pre>
  <p>which should generate something like</p>
  <pre><code>SELECT *
  FROM SOME_ENTITY s
      JOIN ANOTHER_ENTITY a ON s.AE_ID = a.ID
  WHERE s.NAME = :name
      AND a.ID = :ae.ID
  </code></pre>
  <p>In the current migration I already have a MyBatis select query for <code>ANOTHER_ENTITY</code> by id with the corresponding <code>&lt;resultMap&gt;</code></p>
  <p>I'd like to know if there is a way to reuse the <code>AnotherEntity</code>'s <code>&lt;resultMap&gt;</code> to avoid XML mapping duplication and how to identify the columns from the query which correspond to <code>ANOTHER_ENTITY</code> table in the MyBatis's XML mapper file.</p>
  <p>I'm expecting to get something like:</p>
  <pre><code>&lt;mapper namespace=&quot;com.pkg.SomeEntity&quot;&gt;
  
    &lt;select id=&quot;selectSomeEntity&quot; resultMap=&quot;someEntityRm&quot;&gt;
      SELECT *
      FROM SOME_ENTITY s
        JOIN ANOTHER_ENTITY a ON s.AE_ID = a.ID
      WHERE s.NAME = #{name}
    &lt;/select&gt;
  
    &lt;resultMap id=&quot;someEntityRm&quot; type=&quot;com.pkg.SomeEntity&quot;&gt;
      &lt;result property=&quot;id&quot; column=&quot;s.ID&quot;/&gt;
      &lt;result property=&quot;name&quot; column=&quot;s.NAME&quot;/&gt;
      &lt;!-- Assuming that exists mapper XML with namespace=com.pkg.AnotherEntity and resultMap=anotherEntityRm --&gt;
      &lt;result property=&quot;ae&quot; resultMap=&quot;com.pkg.AnotherEntity.anotherEntityRm&quot;/&gt;
    &lt;/resultMap&gt;
  
  &lt;/mapper&gt;
  </code></pre>
  <p>Thanks in advance for your answers</p>