title: Mapping Composition in MyBatis
body: <p>I'm having some trouble with a mapping in MyBatis for Java and would appreciate some help. My class structure is as below:</p>
  <pre><code>//Getters/setters omitted for clarity
  
  class Foo 
  {
    int id;
    Bar bar;
  }
  
  class Bar {
    String x;
    int y;
  }
  </code></pre>
  <p>My table looks like this - i.e. it is de-normalized from the class structure.</p>
  <pre><code>create table foo_bar (
    id int,
    x varchar,
    y int
  );
  </code></pre>
  <p>My working insert statement is able to de-normalize using (bar.x, bar.y) parameters.</p>
  <pre><code>&lt;insert id=&quot;insert&quot; parameterType=&quot;foo&quot;&gt;
    &lt;![CDATA[
      insert into foo_bar
      (id, x, y) values
      (#{x}, #{bar.x}, #{bar.y})
    ]]&gt;
  &lt;/insert&gt;
  </code></pre>
  <p>So, the problem:</p>
  <p><strong>When I execute my select, I would like the resultant object to be an instance of Foo that has a reference to Bar.</strong></p>
  <p>I don't think I can use a type handler since this works over a single column, and and association doesn't seem to make sense since 'Bar' is not an entity explicitly represented in the database via a foreign key relationship.</p>
  <p>Can anyone show me the recommended way of doing this please?</p>
  <p>Thanks!</p>