Right now the procedure at a higher level:
copy a batch of documents in one transaction
for each document in that batch, copy a batch of text flows in one transaction
for each textflows in that batch, copy a batch of text flow targets in one transaction
We could optimize the process by fetch a batch of textflows and all of their targets in memory, and then copy them all. This way we can eliminate the number of sql generated (n+1) and make use of hibernate session cache.
We could also try to reduce the use of collection field on entity, e.g.
. The collection will be populated unnecessarily. We could've just insert the new target directly.