title: Inserting data into table with foreign key using mybatis
body: <p>I have 2 tables, Table 1 has a ID column which is auto generated. I have this Id as a foreign key with table 2, I am using mybatis to insert data into tables. I am stuck at point where I need to send the foreign key to insert command dynamically.</p>
  
  <p>Table 1</p>
  
  <pre><code>CLM_ID (PK) AUTO GENERATED
  CLM_VALUE NN UNIQUE
  </code></pre>
  
  <p>TABLE 2</p>
  
  <pre><code>CLM_ID (FK) 
  CML_VALUE1 (NN)
  CML_VALUE2 (NN)
  CML_VALUE3 (NN)
  </code></pre>
  
  <p>Upon request I am storing the data into table 1 where ID is generated automatically. when I am trying to store the data in table 2 how do I get the {ID}</p>
  
  <p>If I know the value of the corresponding column I can get the ID associated with that column. But how do I pass the column name dynamically.</p>
  
  <p>Sample Mapper example I have.</p>
  
  <pre><code>public class Address {
  
      private Integer address_ID; //PK auto generated
      private String name;
      // getters and setters
  }
  public class Home {
  
      private Integer addressID; //FK
      private String Name;
  }
  
  public interface HomeMapper {
  
      String INSERT_ADDRESS = "INSERT INTO HOMES(ADDRESS_ID, NAME) VALUES ( {addressID}, #{name})";
  
      @Insert(INSERT_ADDRESS)
      @SelectKey(**statement="SELECT ADDRESS_ID FROM ADDRESSES WHERE NAME='Mintu'**", keyProperty = "addressID", before=true, resultType=int.class)
      public void insertRecord(Home homeName);
  
  }
  </code></pre>
  
  <p>How do I send the values dynamically to statement?</p>
  
  <p>Can someone help me to handle this situation? I am new to mybatis and not sure if this is the way to achieve this.</p>