{
if (privateTx) getDatabase().beginTx();
List<E> entities = toList(reader, BATCH_SIZE);
if (writer != null) writer.writeColNames(new EntityTuple(entities.get(0)).getColNames());
while (entities.size() > 0)
{
// resolve foreign keys
this.resolveForeignKeys(entities);
// add to the database
rowsAffected += getDatabase().add(entities);
if (writer != null)
{
for (E entity : entities)
writer.write(new EntityTuple(entity));
}
entities = toList(reader, BATCH_SIZE);
}
if (privateTx) getDatabase().commitTx();