Since starting the long trail back into LD cycling, I have suffered the same types of cramps. Hydration plays a role, but I think that truly it is a matter of getting the miles into your legs. These cramps are coming from exertion by muscles over and over again when they aren't used to it. Climbing does exacerbate the problem, in my experience.
My remedies have been to ride more, pay more attention to rehydration (which is easier said than done) and take electrolyte tabs regularly (or at least salty foods such as crisps/chips).
I have finished rides dehydrated, but the onset of cramps has been held at bay, or at least the twinges have started but not developed into fully-fledged cramps, so riding more under stress (as in at a faster speed or higher gear up hills) does seem to be effective.
I remember one ride when we were returning that had a nasty hill on it. My rehydration routines had gone out the window, I was overdressed (ie, overheated), and by the time we got to the hill, I was ready to call it quits. I walked the hill, tried getting back on the bike, then was paralysed on the roadside for five minutes by a charlie horse cramp that was excruciating. And there was nothing I could do to stop it! It happened several more times... as Machka rode off into the distance oblivious to what was happening. In the end, we ran out of time for the next control, spent an hour or more sitting at a petrol station consuming Coke and chips, then made our way to the finish.
Unfortunately, the cramps were so bad, that the muscle was sore for more than a week afterwards. Now if I feel a twinge, I am very very careful about rehydrating, salt intake, and easing off on the pedal pressure. Sometimes, I will be sitting in a chair at home, go to get up and have the cramp; and climbing into and out of a small tent can be an interesting experience when those muscles decide they don't want to work anymore.