add a type check before doing a cast

https://hudson.grails.org/view/Grails%203.0.x/job/grails_data_mapping_4.x/64/console