The two scriptures you quote are in effect saying the same thing. Please note that in Exodus 20:5 it is speaking of worshiping Idols, while Ezekiel 19:20 is in reference to sin in general.
In Exodus God is it need be noted that God said to the third and fourth generations of those who hate me. where in Ezekiel God is making reference to a person's sins.
In essence they are two different things. where in Exodus it is worship of idols and in Ezekiel they have worship for the true God, but have sinned against God.
An example of Ezekiel is:
2nd Chronicles 6:26 When the heaven is shut up, and there is no rain, because they have sinned against thee; yet if they pray toward this place, and confess thy name, and turn from their sin, when thou dost afflict them; 27 Then hear thou from heaven, and forgive the sin of thy servants, and of thy people Israel, when thou hast taught them the good way, wherein they should walk; and send rain upon thy land, which thou hast given unto thy people for an inheritance.
and an example of Exodus is:
Joshua 7:20 through 26 And Achan answered Joshua, and said, Indeed I have sinned against the LORD God of Israel, and thus and thus have I done: 21 When I saw among the spoils a goodly Babylonish garment, and two hundred shekels of silver, and a wedge of gold of fifty shekels weight, then I coveted them, and took them; and, behold, they are hid in the earth in the midst of my tent, and the silver under it. 22 So Joshua sent messengers, and they ran unto the tent; and, behold, it was hid in his tent, and the silver under it. 23 And they took them out of the midst of the tent, and brought them unto Joshua, and unto all the children of Israel, and laid them out before the LORD. 24 And Joshua, and all Israel with him, took Achan the son of Zerah, and the silver, and the garment, and the wedge of gold, and his sons, and his daughters, and his oxen, and his asses, and his sheep, and his tent, and all that he had: and they brought them unto the valley of Achor. 25 And Joshua said, Why hast thou troubled us? the LORD shall trouble thee this day. And all Israel stoned him with stones, and burned them with fire, after they had stoned them with stones. 26 And they raised over him a great heap of stones unto this day. So the LORD turned from the fierceness of his anger. Wherefore the name of that place was called, The valley of Achor, unto this day.
Earlier in this chapter we see that it was God who pointed to Achan as the culprit. And that even goes to the first of the ten commandments, that is having no other gods before me.