This Mosaic Law on family inheritance is borne out of the story of Jacob and Joseph.
As you may remember, Jacob loved Rachel, and worked 7 years for her uncle, Laban, in order to be able to marry her. Sadly, Jacob must have gotten really drunk, because of the day of his wedding, he finds out that Laban has tricked the trickster, and he's ended up being married to Rachel's older sister, Leah. This being the day of polygamy, Laban says "Hey - don't worry! Just give me another 7 years, and you can have Rachel."
As a dad whose first daughter is named Rachel, I can understand why Jacob would worked 14 years as a bargain. But I digress.
Once Jacob has the two wives, the baby-making war begins. God sees Leah's sorrow and devotion, and so opens her womb first. Jacob's first-born son, Rueben, should, as the first born son, have all the privileges. But, the truth is that Jacob loves Rachel, not Leah. As the story continues, Leah keeps popping out the little ones, but Rachel can't seem to make a baby. Much fuss ensues, and like any good modern reality series goes, Rachel gets Jacob to make babies with her maid, then Leah does the same thing, and before you know it, Jacob has a lot of boys - but Rachel is still not pregnant.
Then, finally, the big day arrives. Rachel gets pregnant, and Jacob is way, way, happy. The boy, Joseph, quickly becomes Jacobs absolute favorite. Hands down. In fact, Jacob loves Joseph so much that he gives him a technicolor coat and arranges for Andrew Lloyd Webber to write about him nearly 4000 years later. Well, we all remember what happens from there - big brothers get angry, sell Joseph off into slavery, and pretty much fill out the rest of Genesis.
(Incidentally - bonus points if you know who Rachel's only other boy is. That's right - Benjamin - the son of his right hand. Rachel dies in childbirth, and when Joseph later uses a ruse to put his only full brother in prison, the other boys realize this would pretty much kill their dad, hence the hysterics to get him back.)
This part of the covenant then, is as much symbolic as it is practical. It is direct, received wisdom from the experience of Jacob and his favorite sons.
Is it possible for a despised wife to have a beloved son? Sure. But we don't have any biblical stories about those prior to this law. The more practical point is "Don't gip your boys in the will," but the precedent that backs it up has its roots in history.