Does having a cavity filled hurt?

Simple: Yes and No! Once a cavity (carious lesion) is diagnosed, the decay will need to be removed and a restoration placed. Typically most cavities are anesthetized and no sensitivity is noted by the patient, just vibration from the handpiece. Larger restorations with infection or cracks can have sensitivity but usually manageable. Be sure to tell dentist if pain so dentist and team can manage discomfort.
No, unless.... Actually, most of the pain from treating cavities is from cleaning out the decay with the dental handpiece. The drill and the cold water/air stimulate a pain response in the pulp. Thankfully, with the local anesthetic that your dentist uses, the only discomfort is from the needle while administering the anesthetic. If you choose to forego local anesthetic, plan on some pain!
Cavity. This really depends on how deep the drilling is going to be. Shallow cavities usually are simple but I have yet to hear of a simple cavity. Hence in general there will be some pain while having it attended to.