No person shall be transferred to the United States Department of Veterans Affairs or other federal agency if he or she is confined pursuant to the conviction of any felony or misdemeanor or if he or she has been acquitted of the charge solely on the ground of insanity, unless prior to transfer the court placing such person enters an order for the transfer after appropriate motion and hearing and