简体   繁体   中英

Issues with UUID with Grails, GORM and MongoDB

I am new to Grails and I tried to persist a model with the id of type UUID. I am aware of that there is ObjectId but for now I would to persist UUID.

class Book {
  UUID id
  String title
}

I have a BookController where I have enabled scaffolding. When I do now load the controller and try to add a new book, I get the following error message from 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

I set a breakpoint at the org.grails.datastore.mapping.engine.NativeEntryEntityPersister. When the application gets there, I can resolve the getMappingContext().getConversionService() part which returns an instance of org.grails.datastore.mapping.model.types.conversion.DefaultConversionService. This instance has 10 converters configured including converters for ObjectId and Binary. I am asking myself now, where I can configure my own converter so that I can convert between String and UUID?

As I am new to Grails I have no clue where to put the required Java files and in which file I have to put what kind of configuration. Would be cool if someone could point me in the right direction.

There's a session in the docs for custom user types .

So you need to create your type class that extends AbstractMappingAwareCustomTypeMarshaller and register as a Spring Bean (see in the final of the docs).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM