The JDBC batch sorting algorithm has undergone many modifications to address various entity-relationship models.
Initially, the sorting was done in a single main traversal with previous/next checks. Because this was problematic, we changed to a bubble sort-like implementation which makes it useless to check the previous entries.
Therefore, we should simplify to only check the next statement and rearrange it if it's a parent entity that needs to be persisted first.
Fixed in 5.1 branch as well.