我无法找到这个问题的正确答案。 我有mongodb的grail应用程序设置。我可以看到数据库已经创建。
我有一个域类定义如下:
class Survey {
int numPages
Map<String, Page> pages = new HashMap<String, Page>();
static mapWith = "mongo"
static constraints = {
}
}
Survey类构建了许多页面(通过读取json数据)。所有数据都按预期填充。 问题是将调查保存到数据库时:
survey.save(flush:true)
我得到一个例外,据我所知,这与GROM不喜欢HashMap字段页的事实有关... 完整的堆栈跟踪:
2017-05-02 16:36:19.735 ERROR --- [nio-8080-exec-6] o.g.web.errors.GrailsExceptionResolver : GroovyCastException occurred when processing request: [POST] /survey/submitSurvey
Cannot cast object '{page2=com.fsc.Page : (unsaved), page1=com.fsc.Page : (unsaved), page0=com.fsc.Page : (unsaved)}' with class 'java.util.HashMap' to class 'java.lang.Iterable' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.lang.Iterable(java.util.HashMap). Stacktrace follows:
java.lang.reflect.InvocationTargetException: null
at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:210)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:187)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{page2=com.fsc.Page : (unsaved), page1=com.fsc.Page : (unsaved), page0=com.fsc.Page : (unsaved)}' with class 'java.util.HashMap' to class 'java.lang.Iterable' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.lang.Iterable(java.util.HashMap)
at org.grails.datastore.mapping.mongo.engine.MongoCodecEntityPersister.processAssociations(MongoCodecEntityPersister.groovy:359)
at org.grails.datastore.mapping.mongo.engine.MongoCodecEntityPersister.persistEntity(MongoCodecEntityPersister.groovy:218)
at org.grails.datastore.mapping.mongo.engine.MongoCodecEntityPersister.persistEntity(MongoCodecEntityPersister.groovy:537)
at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:183)
at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:598)
at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:356)
at org.grails.datastore.gorm.GormInstanceApi$_save_closure5.doCall(GormInstanceApi.groovy:177)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:318)
at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:40)
at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:176)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:151)
at com.fsc.SurveyService.saveSurvey(SurveyService.groovy:114)
at com.fsc.SurveyController.$tt__submitSurvey(SurveyController.groovy:153)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
... 14 common frames omitted
任何想法如何解决这个问题?如果它与我的Survey类定义有关,我确实需要一个hashmap,因为键在设计中起着重要作用。谢谢!