title: Does JDBC getGeneratedKeys() method always same order of inserted element
body: <p>I use <code>executeBatch()</code> with JDBC to insert multiple rows and I want to get id of inserted rows for another insert I use this code for that purpose:</p>
  
  <pre><code>insertInternalStatement = dbConncetion.prepareStatement(INSERT_RECORD, generatedColumns);
  
  for (Foo foo: foosHashSet) {
  
      insertInternalStatement.setInt(1, foo.getMe());
      insertInternalStatement.setInt(1, foo.getMe2());
      // ..
      insertInternalStatement.addBatch();
  }
  insertInternalStatement.executeBatch();
  
  // now get inserted ids
  try (ResultSet generatedKeys = insertInternalStatement.getGeneratedKeys()) {
  
       Iterator&lt;Foo&gt; fooIterator= foosHashSet.iterator();
       while (generatedKeys.next() &amp;&amp; fooIterator.hasNext()) {
  
           fooIterator.next().setId(generatedKeys.getLong(1));
  
       }
   }
  </code></pre>
  
  <p>It works fine and ids are returned, my question are:</p>
  
  <ol>
  <li><p>if I iterate over <code>getGeneratedKeys()</code> and <code>foosHashSet</code> will ids return in same order so that each returned id from database belongs to corresponding Foo instance? </p></li>
  <li><p>What about when I use multi thread and above code run in multiple threads simultaneously?</p></li>
  <li><p>Is there any other solution for this? I have two table foo1 and foo2 and I want first insert foo1 records then use their primary ids as foo2 foreign key.</p></li>
  </ol>