My boyfriend had a hernia his testicle is painful everytime we had sex and its been 3years now. What to do?

Nerve pain. The mesh or scar tissue may have entrapped or is placing pressure on the ilioinguinal or genitofemoral nerves that run thru the hernia canal. See a pain md or surgeon for a nerve block. If this works but does not last (which after 3 years it may not) you can have a neurectomy (cut the nerve) and the pain disappears. He will have some numbness in that area. Freeing the nerve is more difficult.
See a surgeon. He needs to see a general surgeon to have this fixed if its causing pain during sex. Parts of the small bowel can herniate through the inguinal canal and get incarcerated in the scrotum necessitating reduction and mesh to prevent displacement again.
Treatment is availab. Hernia can be treated simply by surgery. He should see his doctor to discuss surgery .