title: MyBatis: How to map "inverse" relationship?
body: <p>My problem is to persist two classes that have a 1:n relationship:</p>
  
  <pre><code>public class DayRecord {
      private Long id;
      private List&lt;TimeRecord&gt; timeRecordsToday = new ArrayList&lt;TimeRecord&gt;(4);
      ...
  }
  
  public class TimeRecord {
      private Long id;
      ...
  }
  </code></pre>
  
  <p>So, in code, DayRecord knows TimeRecord.</p>
  
  <pre><code>create table DAY_RECORDS (
      id int primary key,
  );
  
  create table TIME_RECORDS (
      id int primary key,
      day_record_id int not null,
      foreign key (day_record_id) references DAY_RECORDS (id)
  );
  </code></pre>
  
  <p>In database, TimeRecord knows DayRecord.</p>
  
  <p><strong>Can I save a DayRecord with all its TimeRecords in one step?</strong></p>
  
  <p>In Hibernate, I can set an inverse mapping and just save a DayRecord and all its TimeRecords will get saved, too. With MyBatis, I tried to save the classes independently from each other:</p>
  
  <pre><code>&lt;mapper
      namespace="de.stevenschwenke.java.javafx.xyz.DayRecordMapper"&gt;
      &lt;insert id="insertDayRecord" 
          parameterType="de.stevenschwenke.java.javafx.xyz.DayRecord"&gt;
          insert into DAY_RECORDS (id) values (NEXT VALUE FOR DAY_RECORDS_SEQ);
      &lt;/insert&gt;
  &lt;/mapper&gt;
  
  &lt;mapper
      namespace="de.stevenschwenke.java.javafx.xyz.TimeRecordMapper"&gt;
      &lt;insert id="insertTimeRecord"
          parameterType="de.stevenschwenke.java.javafx.xyz.TimeRecord"&gt;
          insert into TIME_RECORDS (id) values (NEXT VALUE FOR TIME_RECORDS_SEQ);
      &lt;/insert&gt;
  &lt;/mapper&gt;
  </code></pre>
  
  <p>But how can I save the DayRecord-ID inTimeRecord?</p>
  
  <p>Ideas: </p>
  
  <ol>
  <li>Give TimeRecord an attribute dayRecordId. This way, a cyclic dependency would be created. However, the mapping would take care of the dpenedency while saving.</li>
  <li>In one transaction, save the DayRecord first, get its ID, set it in TimeRecords and save this object.</li>
  <li>use a nested select-statement within insert like in the documentation</li>
  </ol>
  
  <p>What is the best way to save both objects? Thanks for your help!</p>