使用Grails,GORM和MongoDB的UUID问题

时间:2013-02-21 19:48:51

标签: mongodb grails gorm

我是Grails的新手,我试图坚持使用类型为UUID的模型。我知道有ObjectId,但现在我会坚持使用UUID。

class Book {
  UUID id
  String title
}

我有一个BookController,我启用了脚手架。当我现在加载控制器并尝试添加新书时,我从Grails收到以下错误消息:

Error 500: Internal Server Error
URI /GrailsTest/book/save
Class org.springframework.core.convert.ConverterNotFoundException
Message No converter found capable of converting from type java.lang.String to type java.util.UUID
Around line 24 of grails-app/controllers/ch/teamrg/test/BookController.groovy
Around line 186 of PageFragmentCachingFilter.java
183:            if(method == null) {
184:                log.debug("No cacheable method found for {}:{} {}",
185:                        new Object[] { request.getMethod(), request.getRequestURI(), getContext() });
186:                chain.doFilter(request, response);
187:                return;
188:            }
189:            Collection cacheOperations = cacheOperationSource.getCacheOperations(
Around line 63 of AbstractFilter.java
60:     try {
61:         // NO_FILTER set for RequestDispatcher forwards to avoid double gzipping
62:         if (filterNotDisabled(request)) {
63:             doFilter(request, response, chain);
64:         }
65:         else {
66:             chain.doFilter(req, res);
Trace
   Line | Method
->>  18 | convert          in org.grails.datastore.mapping.model.types.conversion.DefaultConversionService
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   365 | cacheNativeEntry in org.grails.datastore.mapping.engine.NativeEntryEntityPersister
|   725 | persistEntity .  in     ''
|   125 | persist          in org.grails.datastore.mapping.engine.EntityPersister
|   479 | persist . . . .  in org.grails.datastore.mapping.core.AbstractSession
|   166 | doSave           in org.grails.datastore.gorm.GormInstanceApi
|   143 | doCall . . . . . in org.grails.datastore.gorm.GormInstanceApi$_save_closure4
|   301 | execute          in org.grails.datastore.mapping.core.DatastoreUtils
|    34 | execute . . . .  in org.grails.datastore.gorm.AbstractDatastoreApi
|   142 | save             in org.grails.datastore.gorm.GormInstanceApi
|   258 | call . . . . . . in org.grails.datastore.gorm.InstanceMethodInvokingClosure
|    24 | save             in BookController.groovy
|   186 | doFilter . . . . in PageFragmentCachingFilter.java
|    63 | doFilter         in AbstractFilter.java
|   886 | runTask . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run              in     ''
^   680 | run . . . . . .  in java.lang.Thread

我在org.grails.datastore.mapping.engine.NativeEntryEntityPersister设置了一个断点。当应用程序到达那里时,我可以解析getMappingContext()。getConversionService()部分,该部分返回org.grails.datastore.mapping.model.types.conversion.DefaultConversionService的实例。该实例具有10个配置的转换器,包括用于ObjectId和Binary的转换器。我现在问自己,在哪里可以配置我自己的转换器,以便我可以在String和UUID之间进行转换?

由于我是Grails的新手,我不知道在哪里放置所需的Java文件以及在哪个文件中我需要放置什么样的配置。如果有人能指出我正确的方向,那会很酷。

1 个答案:

答案 0 :(得分:0)

custom user types的文档中有一个会话。

因此,您需要创建扩展AbstractMappingAwareCustomTypeMarshaller的类型类并注册为Spring Bean(请参阅文档的最后部分)。