Beethoven wrote the Eroica for Napoleon, and Shostakovich’s 5th is dedicated to the Russian communist revolution of 1917. Both are beautiful.
He did because he rightly thought Napoleon was going to save France from the Terror. But after seeing Napoleon's imperialist actions, he scratched out the dedication and replaced it with "to a once great man." He came to hate Napoleon.