Substituir chave primária (i) de domínio por UUID
02/09/2016 15:05
0
Prezados,
Como fazer para substituir o id de um domínio de Long para UUId?
Minha versão do Grails é 2.5.3 e o banco de dados é o H2.
Tentei algumas abordagens mostradas na internet porém sem sucesso.
Estou recebendo o erro "Application is null".

Obrigado
Tags: Grails,uuid,chave primária, id


1
Oi Pedro,

há duas maneiras: a primeira é configurando a coluna id para que o gerador seja do tipo assigned, tal como no exemplo abaixo:


class Entidade {
   String id = UUID.randomUUID().toString() // defino manualmente

   static mapping = {
              id generator:'assigned'
   }
}


Note que desta maneira posso usar o gerador que eu quiser, independente de existir uma implementação para o mesmo no Hibernate ou não.

A segunda é usando o generator do tipo uuid do Hibernate de forma direta


class Entidade {
     String id // repare que preciso declarar do tipo string aqui

    static mapping = {
              id generator: 'uuid'
     }
}


0
Olá Kiko, tentei usar as duas abordagens, mas deu erro.
O primeira abordagem dá o seguinte erro:

2016-09-06 09:22:10,697 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: null
Message: null

Line | Method
->>  901 | checkForComodification       in java.util.ArrayList$Itr
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    851 | next                         in     ''
|   1042 | next . . . . . . . . . . . . in java.util.Collections$UnmodifiableCollection$1
|    461 | executeActions               in org.hibernate.engine.spi.ActionQueue
|    349 | executeActions . . . . . . . in     ''
|    350 | performExecutions            in org.hibernate.event.internal.AbstractFlushingEventListener
|     56 | onFlush . . . . . . . . . .  in org.hibernate.event.internal.DefaultFlushEventListener
|   1222 | flush                        in org.hibernate.internal.SessionImpl
|     87 | flushSession . . . . . . . . in org.codehaus.groovy.grails.orm.hibernate.metaclass.SavePersistentMethod
|     60 | doInHibernate                in org.codehaus.groovy.grails.orm.hibernate.metaclass.SavePersistentMethod$1
|    179 | doExecute . . . . . . . . .  in org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate
|    123 | execute                      in     ''
|     56 | performSave . . . . . . . .  in org.codehaus.groovy.grails.orm.hibernate.metaclass.SavePersistentMethod
|    215 | doInvokeInternal             in org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractSavePersistentMethod
|     68 | invoke . . . . . . . . . . . in org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractDynamicPersistentMethod
|    195 | save                         in org.codehaus.groovy.grails.orm.hibernate.HibernateGormInstanceApi
|     -1 | save . . . . . . . . . . . . in config.TrustInstalacao
|     48 | defaultCall                  in org.codehaus.groovy.runtime.callsite.CallSiteArray
|    113 | call . . . . . . . . . . . . in org.codehaus.groovy.runtime.callsite.AbstractCallSite
|    125 | call                         in     ''
|     81 | carregarTeste . . . . . . .  in config.TrustInstalacaoService
|     -1 | call                         in config.TrustInstalacaoService$carregarTeste
|     48 | defaultCall . . . . . . . .  in org.codehaus.groovy.runtime.callsite.CallSiteArray
|    113 | call                         in org.codehaus.groovy.runtime.callsite.AbstractCallSite
|    117 | call . . . . . . . . . . . . in     ''
|     90 | configureForDevelopment      in BootStrap
|     -2 | invoke0 . . . . . . . . . .  in sun.reflect.NativeMethodAccessorImpl
|     62 | invoke                       in     ''
|     43 | invoke . . . . . . . . . . . in sun.reflect.DelegatingMethodAccessorImpl
|    498 | invoke                       in java.lang.reflect.Method
|   1426 | jlrMethodInvoke . . . . . .  in org.springsource.loaded.ri.ReflectiveInterceptor
|     93 | invoke                       in org.codehaus.groovy.reflection.CachedMethod
|    325 | doMethodInvoke . . . . . . . in groovy.lang.MetaMethod
|   1210 | invokeMethod                 in groovy.lang.MetaClassImpl
|   1123 | invokeMethod . . . . . . . . in groovy.lang.ExpandoMetaClass
|   1019 | invokeMethod                 in groovy.lang.MetaClassImpl
|   1155 | invokeMethod . . . . . . . . in     ''
|   1123 | invokeMethod                 in groovy.lang.ExpandoMetaClass
|   1019 | invokeMethod . . . . . . . . in groovy.lang.MetaClassImpl
|    810 | invokeMethod                 in     ''
|     46 | invokeMethod . . . . . . . . in groovy.lang.GroovyObjectSupport
|     -1 | invokeMethod                 in BootStrap$_closure1
|   1275 | invokeMethodOnGroovyObject . in groovy.lang.MetaClassImpl
|   1169 | invokeMethod                 in     ''
|   1123 | invokeMethod . . . . . . . . in groovy.lang.ExpandoMetaClass
|   1019 | invokeMethod                 in groovy.lang.MetaClassImpl
|    810 | invokeMethod . . . . . . . . in     ''
|     46 | invokeMethod                 in groovy.lang.GroovyObjectSupport
|     -1 | invokeMethod . . . . . . . . in BootStrap$_closure1$_closure3
|   1275 | invokeMethodOnGroovyObject   in groovy.lang.MetaClassImpl
|   1169 | invokeMethod . . . . . . . . in     ''
|   1123 | invokeMethod                 in groovy.lang.ExpandoMetaClass
|   1019 | invokeMethod . . . . . . . . in groovy.lang.MetaClassImpl
|     69 | callCurrent                  in org.codehaus.groovy.runtime.callsite.PogoMetaClassSite
|     52 | defaultCallCurrent . . . . . in org.codehaus.groovy.runtime.callsite.CallSiteArray
|    154 | callCurrent                  in org.codehaus.groovy.runtime.callsite.AbstractCallSite
|    158 | callCurrent . . . . . . . .  in     ''
|     75 | doCall                       in BootStrap$_closure1$_closure3$_closure6
|     -1 | doCall . . . . . . . . . . . in     ''
|     -2 | invoke0                      in sun.reflect.NativeMethodAccessorImpl
|     62 | invoke . . . . . . . . . . . in     ''
|     43 | invoke                       in sun.reflect.DelegatingMethodAccessorImpl
|    498 | invoke . . . . . . . . . . . in java.lang.reflect.Method
|   1426 | jlrMethodInvoke              in org.springsource.loaded.ri.ReflectiveInterceptor
|     93 | invoke . . . . . . . . . . . in org.codehaus.groovy.reflection.CachedMethod
|    325 | doMethodInvoke               in groovy.lang.MetaMethod
|   1210 | invokeMethod . . . . . . . . in groovy.lang.MetaClassImpl
|   1123 | invokeMethod                 in groovy.lang.ExpandoMetaClass
|   1019 | invokeMethod . . . . . . . . in groovy.lang.MetaClassImpl
|    426 | call                         in groovy.lang.Closure
|     -1 | call . . . . . . . . . . . . in BootStrap$_closure1$_closure3$_closure6
|    420 | call                         in groovy.lang.Closure
|     -1 | call . . . . . . . . . . . . in BootStrap$_closure1$_closure3$_closure6
|    340 | execute                      in grails.util.Environment$EnvironmentBlockEvaluator
|    321 | executeForEnvironment . . .  in grails.util.Environment
|    296 | executeForCurrentEnvironment in     ''
|     60 | callInit . . . . . . . . . . in org.codehaus.groovy.grails.commons.DefaultGrailsBootstrapClass
|     78 | executeGrailsBootstraps      in org.codehaus.groovy.grails.web.context.GrailsConfigUtils
|    110 | initWebApplicationContext .  in org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener
|    106 | contextInitialized           in org.springframework.web.context.ContextLoaderListener
|   4750 | listenerStart . . . . . . .  in org.apache.catalina.core.StandardContext
|   5197 | startInternal                in     ''
|    155 | start . . . . . . . . . . .  in org.apache.catalina.util.LifecycleBase
|   1404 | call                         in org.apache.catalina.core.ContainerBase$StartChild
|   1394 | call . . . . . . . . . . . . in     ''
|    266 | run                          in java.util.concurrent.FutureTask
|   1142 | runWorker . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    617 | run                          in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . . . . . . . . . .  in java.lang.Thread
Disconnected from the target VM, address: '127.0.0.1:50397', transport: 'socket'
Error |
Forked Grails VM exited with error
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=256m; support was removed in 8.0

Process finished with exit code 1


.................................
Obrigado!
06/09/2016 09:27



Ainda não faz parte da comunidade???

Para se registrar, clique aqui.


Aprenda Groovy e Grails com a Formação itexto!

Newsletter Semana Groovy

Assinar

Envie seu link!


Livro de Grails


/dev/All

Os melhores blogs de TI (e em português) em um único lugar!

 
Creative Commons
RSS Grails Brasil é mantido por itexto Consultoria.
Em caso de problemas contacte Henrique Lobo Weissmann (Kico) por e-mail: kico@itexto.com.br
Todo o conteúdo presente neste site adota o Creative Commons como licença padrão.
Ver: 4.14.0
itexto