title: Spring RowMapper for OneToMany?
body: <p>I am trying to adapt two RowMappers for two objects which have @OneToMany relationship between them.</p>
  
  <p>Assume, I have two methods:</p>
  
  <pre><code>public Account findAccount(long id) {
      SQL = "SELECT * FROM accounts WHERE id = ?";
      Account account = template.queryForObject(SQL, new Object[] { id }, MAP_ACCOUNT);
      return account;
  }
  
  
  public Card findCard(String number) {
      SQL = "SELECT * FROM cards WHERE number = ?";
      Card card = template.queryForObject(SQL, new Object[] { number }, MAP_CARD);
      return card;
  }
  </code></pre>
  
  <p>And two row mappers: </p>
  
  <pre><code>   private final RowMapper&lt;Card&gt; MAP_CARD = new RowMapper&lt;Card&gt;() {
  
      public Card mapRow(ResultSet rs, int rowNum) throws SQLException {
          Account account = findAccount(rs.getLong("account_id"));
          Card card = new DefaultCard(rs.getString("number"), account);
          return card;
      }
  
  };
  
  private final RowMapper&lt;Account&gt; MAP_ACCOUNT = new RowMapper&lt;Account&gt;() {
  
      public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
          SQL = "SELECT * FROM cards where account_id = " + rs.getLong("id");
          List&lt;Card&gt; cards = template.query(SQL, MAP_CARD);
          Account account = new DefaultAccount(rs.getLong("id"), rs.getString("username"), cards);
          return account;
      }
  };
  </code></pre>
  
  <p>Running findAccount or findCard methods will throw an exception stating "Too many connections!". This is because of the cyclic-dependency of row mappers between them. I know I've done this the wrong way and I would like to know how to correctly rewrite the row mappers. Many thanks.</p>