Psalm 51 helps us understand the heart of David and his allegiance to God after he so grievously sinned against God and all his family.
And when her mourning for Uhriah was over, David sent and brought Bathsheba to his house and she became his wife… Then David comforted his wife Bathsheba, and he went to her and lay with her. She gave birth to a son, and they named him Solomon. 2 Samuel 12:24
2 Samuel 12:15:25 David’s intense grieving, telling of David’s suffering over his sin and the death of his son. It is one of the more moving passages in the Bible. David writes the 51st Psalm, though he had been assured that his sin was pardoned, prays earnestly for pardon and greatly laments his sin. He was willing to bear the shame of it and have it go ever before him. David submitted to the will of God in the death of one child, and God made up the loss to his advantage, in the birth of another. The way to see our our peace and comfort return back to us in tough situations , or the restoring of the loss made up some other way, is “cheerfully” to resign all to God. God, by his grace, had ordered David’s new born son to be called Jedidiah–Beloved of the Lord.