Semantic parsing in KBQA aims to parse natural language questions into logical forms, whose execution against a knowledge base produces answers. Learning semantic parsers from question-answer pairs requires searching over a huge space of logical forms for ones consistent with answers. Current methods utilize various prior knowlege or entity-level KB constraints to reduce the search space. In this paper, we investigate for the first time prior knowledge from external logical form annotations and ontology-level constraints. We design a hierarchical architecture for program transfer, and propose an ontology-guided pruning algorithm to reduce the search space. The experiments on ComplexWebQuestions show that our method improves the state-of-the-art F1 score from 44.0% to 58.7%, with an absolute gain of 14.7%, which demonstrates the effectiveness of program transfer and ontology awareness.