Upvote:0
The problem here is your perspective. It appears that you don't allow G-d to do as he pleases in that David's punishment was the death of the child. The child isn't punished. G-d does not guarantee anyone any term of life. Life belongs to G-d and he can do with it whatever he chooses. As an innocent, the child is guaranteed eternal life.
So G-d is not unjust because the child wasn't treated unjustly.
G-d doesn't punish the child for the sin of the parent but there is a principle called collateral damage. If I take LSD and damage my genes, then my child will suffer for my action. In this case David choose evil and David's punishment was the death of the child. This is not unjust on G-d's part.
Secondly forgiveness of sin doesn't alleviate the consequence of sin. If I rob a bank and repent the judge isn't going to commute my sentence.
I hope this helps.