【问题】
view(gsp页面)上有select box,在Controller里面调用xxxInstance.properties = params时,会报错:
{{org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Error occurred creating command object.
....
....
Caused by: java.lang.ClassCastException: B$_clinit_closure1 cannot be cast to java.util.Map
at org.codehaus.groovy.grails.web.binding.GrailsDataBinder.getConstrainedPropertyForPropertyValue(GrailsDataBinder.java:309)
【临时解决办法】
Step1.升级到最新的hibernate 1.1.3-SNAPSHOT
grails install-plugin hibernate 1.1.3-SNAPSHOT
Step2. putting the following in BootStrap
private static convertToType(value, targetType) {
SimpleTypeConverter typeConverter = new SimpleTypeConverter()
if (value != null && !targetType.isAssignableFrom(value.class)) {
if (value instanceof Number && Long.class.equals(targetType)) {
value = value.toLong()
}
else {
try {
value = typeConverter.convertIfNecessary(value, targetType)
} catch (org.springframework.beans.TypeMismatchException e) {
// ignore
};
}
}
return value
}
def init = { servletContext ->
//---------------------------------------------------------------------
// FIX BUG GRAILS-5445
//---------------------------------------------------------------------
def ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext)
if(ctx.containsBean("sessionFactory")) {
def sf = ctx.getBean("sessionFactory")
def template = new HibernateTemplate(sf)
for(d in ctx.getBean("grailsApplication").domainClasses) {
def domain = d
def identityType = domain.identifier.type
domain.clazz.metaClass.static.get = { Serializable id ->
id = convertToType(id, identityType)
def obj = id ? template.get(domain.clazz,id) : null
if(obj && obj instanceof org.hibernate.proxy.HibernateProxy) {
obj = obj.getHibernateLazyInitializer().implementation
}
return obj
}
}
}
如果Step1能解决问题,则不需要进行step2了。
【结论及其他】
1. 升级一个小版本号就引发这么大的问题,实在让人心寒,不过答复倒是挺快,临时解决方案也有了
2. 官方答复在1.1.3中修复此问题
3. 本问题Jira地址:http://jira.codehaus.org/browse/GRAILS-5445