我正在尝试在Grails中动态创建域对象,并遇到这样的问题:对于引用另一个域对象的任何属性,metaproperty告诉我它的类型是“java.lang.Object”而不是期望的类型 .
例如:
class PhysicalSiteAssessment {
// site info
Site site
Date sampleDate
Boolean rainLastWeek
String additionalNotes
...
是域类的开头,它引用另一个域类“站点” .
如果我尝试使用此代码(在服务中)动态查找此类的属性类型:
String entityName = "PhysicalSiteAssessment"
Class entityClass
try {
entityClass = grailsApplication.getClassForName(entityName)
} catch (Exception e) {
throw new RuntimeException("Failed to load class with name '${entityName}'", e)
}
entityClass.metaClass.getProperties().each() {
println "Property '${it.name}' is of type '${it.type}'"
}
然后结果是它识别Java类,但不识别Grails域类 . 输出包含以下行:
Property 'site' is of type 'class java.lang.Object'
Property 'siteId' is of type 'class java.lang.Object'
Property 'sampleDate' is of type 'class java.util.Date'
Property 'rainLastWeek' is of type 'class java.lang.Boolean'
Property 'additionalNotes' is of type 'class java.lang.String'
问题是我想使用动态查找来查找匹配的对象,例如做一个
def targetObjects = propertyClass."findBy${idName}"(idValue)
通过内省检索propertyClass的地方,idName是要查找的属性的名称(不一定是数据库ID),idValue是要查找的值 .
一切都以:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static java.lang.Object.findByCode() is applicable for argument types: (java.lang.String) values: [T04]
有没有办法找到属性的实际域类?或者可能是一些其他解决方案来查找未给出类型的域类的实例(只有具有该类型的属性名称)?
如果我使用类型名称是大写的属性名称(“site” - >“Site”)以通过grailsApplication实例查找类的约定,它可以工作,但我想避免这种情况 .