我正在尝试使用Grails Criteria来获取Mongodb中hasMany关系的结果,但是没有得到正确的结果。我不确定代码有什么问题,或者我错过了什么。
My Grails版本为3.3.2,mongodb插件版本为6.1.2。
我的域名类是:
SampleDoc.groovy
class SampleDoc implements Serializable{
static mapWith = "mongo"
static belongsTo = [ samplefile : SampleFile ]
static hasMany = [sampleDocElements : SampleDocElement ]
static {
JSON.registerObjectMarshaller(SampleDoc) {
Map properties = it.properties.findAll {it}
properties.put("id", it.id.toString())
return properties
}
}
}
SampleFile.groovy
class SampleFile implements Serializable{
static mapWith = "mongo"
String name
static {
JSON.registerObjectMarshaller(SampleFile) {
Map properties = it.properties.findAll {it}
properties.put("id", it.id.toString())
return properties
}
}
}
SampleDocElement.groovy
class SampleDocElement implements Serializable{
static mapWith = "mongo"
String value
static {
JSON.registerObjectMarshaller(SampleDocElement) {
Map properties = it.properties.findAll {it}
properties.put("id", it.id.toString())
return properties
}
}
}
以下标准不起作用:
SampleDoc.createCriteria().list{
eq ('samplefile', samplefile)
sampleDocElements {
ilike ('value', value)
}
}
我收到错误:
Error :groovy.lang.MissingMethodException:
No signature of method:
org.grails.datastore.mapping.mongo.query.MongoQuery$MongoResultList.call()
is applicable for argument types:
(test.SampleDocService$__tt__getSampleDocs_closure45$_closure106)
values:
[test.SampleDocService$__tt__getSampleDocs_closure45$_closure106@7925]
Possible solutions: tail(), tail(), wait(), max(), last(), last()
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at test.SampleDocService$_tt_getSampleDocs_closure45.doCall(SampleDocService.groovy:477)
at test.SampleDocService$_tt_getSampleDocs_closure45.doCall(SampleDocService.groovy
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1427)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.Closure.call(Closure.java:414)
at test.SampleDocService$__tt__getSampleDocs_closure45.call(SampleDocService.groovy)
at groovy.lang.Closure.call(Closure.java:408)
at test.SampleDocService$__tt__getSampleDocs_closure45.call(SampleDocService.groovy)
at org.grails.datastore.gorm.query.criteria.AbstractCriteriaBuilder.invokeClosureNode(AbstractCriteriaBuilder.java:1056)
at grails.gorm.CriteriaBuilder.list(CriteriaBuilder.java:105)
at grails.gorm.CriteriaBuilder.list(CriteriaBuilder.java:42)
at org.grails.datastore.mapping.query.api.BuildableCriteria$list$1.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at .SampleDocService.$tt__getSampleDocs_closure45(SampleDocService.groovy:475)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1427)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:925)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:908)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethodSafe(InvokerHelper.java:76)
at test.SampleDocService$_getSampleDocs_closure45.doCall(SampleDocService.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1427)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.Closure.call(Closure.java:414)
at test.SampleDocService$_getSampleDocs_closure45.call(SampleDocService.groovy)
at groovy.lang.Closure.call(Closure.java:430)
使用Grails findAllBy不会抛出任何错误,但是为空而不是正确的resultSet:
SampleDoc.findAllBySampleFileAndSampleDocElementsInList(sampleFile, sampleDocElements)