title: Batch insert into multiple table with sequence
body: <p>We are facing a problem while implementing a batch insert code in our java application. Below is our table structure -</p>
  
  <ul>
  <li>We have three tables : Table1, Table2 &amp; Table3. </li>
  <li>Primary key of Table1 (SeqId, which is essentially sequence) is foregn key for rest two tables (Table2 &amp; Table3).</li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/95J3y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/95J3y.png" alt="Table Structure"></a></p>
  
  <p><strong>Current Implementation :</strong></p>
  
  <p>As part of current batch operation, for each iteration we are inserting sequentially in Table1 first followed by Table2 &amp; Table3.</p>
  
  <p>[Pseudo Code]</p>
  
  <pre><code>INSERT INTO Table1(SeqId, OtherField1) VALUES (ID_SEQ.nextval, 'Some Val');
  INSERT INTO Table2(SeqId, OtherField1) VALUES (someId2, ID_SEQ.currval, 'Some Val');
  INSERT INTO Table3(SeqId, OtherField1) VALUES (someId3, ID_SEQ.currval, 'Some Val');
  </code></pre>
  
  <p><strong>What we are trying :</strong></p>
  
  <p>Now we are trying to implement batch insert. We have created three seperate PreparedStatement and for each iteration we are doing <code>statement.addBatch();</code> and at the very end we are executing <code>statement.executeBatch()</code> sequentially. </p>
  
  <p>Now the problem is <code>ID_SEQ.currval</code> will always take the current sequence value which is not what we want. Agaist each value of SeqId in Table1, we want correponding row in Table2 and Table3.</p>
  
  <p>Is it possible to implement this using batch insert (not a procedure or anonymous block)?</p>
  
  <p>We are using Oracle <code>11g</code> and <code>Java8</code></p>