How do I prevent tooth pain after getting a composite filling?

How long? If your tooth is painful more then a day or two after a composite filling it is best to see the dentist who did it to evaluate the bite and see if the nerve has been affected.
Prevent? Proper dental techniques reduces the chance of post operative discomfort following the placement of composite fillings, but even with that, sometimes there is some sensitivity or discomfort. You can't really prevent it. However, if it does occur, see your dentist to see if the bite needs adjusting or the tooth needs further treatment.
Mild pain is normal. However, if the pain continues for two weeks, there is a problem with your composite filling and you should see your dentist. Good luck!
See your dentist. Please go back to see the dentist who placed the composite filling. Perhaps it is too high and you are biting into it too hard. Perhaps the filling is cracked or did not bond well. Perhaps the dentist may assist you with a mild pain medication. I would go back to the dentist.