Using an uuid or guid as id in grails/hibernate

不问归期 提交于 2020-01-01 11:45:13

问题


I need to have GUID/UUID's as the id column of my rows.

This is to be able to create entries both online and offline, and of course not having these conflict on PK when merging. I know i could mitigate this, but i'd like to keep it simple, and there is legacy apps already using uuid/guids to define relationships). Data also needs to be synced both ways later. Rewriting existing applications is not an option.

When i try to use either GUID or UUID with grails i get an error 500. (using a GUID on h2 results in another error - detaling that DB does not support GUIDs, as expected).

I get this error when i try to save an 'WithUUID':

URI    /gtestUUID/withUUID/save
Class    java.lang.IllegalArgumentException
Message    argument type mismatch

Entire Error 500: http://imgur.com/m2Udbm6.png

I've tried with mariadb 5.5 and 1.1.7 driver, this results in the same problem.

Grails 2.3.8. Windows 8.1 (x64). Netbeans 7.4

all default.

Example classes:

WithUUID.groovy:

package gtestuuid

class WithUUID {
    String name
    static constraints = {
    }
    static mapping = {
        id generator: 'uuid'
    }
}

WithUUIDController.groovy:

package gtestuuid

class WithUUIDController {
    def scaffold = WithUUID
}

Any help would be greatly appreciated.

Link to relevant grails documentation

(i'm not able to post links to more docs/posts due to my low rep)


回答1:


You need to set the id as String or UUID (or anything you need).

Below, an example of my class User with another possibility:

import java.util.UUID

class User {

    String id = UUID.randomUUID().toString()

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



回答2:


Or you can specify UUID id field in your class without any initializing and set mapping like in this case

class Buyer {

    UUID id
    String name
    String phone
    String email

    static constraints = {
    }

    static mapping = {
        id generator : 'uuid2', type: 'pg-uuid' // pg-uuid because I use postgresql
    }

}

You can take out mapping in Config.groovy

grails.gorm.default.mapping = {
    id generator : 'uuid2', type: 'pg-uuid'
}

and your classes will contain only UUID id field without any redudant code




回答3:


Here's what I did in my Grails 3 domain class...

class MyClass {    
    static constraints = {
    }

    UUID id
    static mapping = {
        id generator: "UuidGenerator", length: 36, type: "uuid-char"

   }


}

Then here's what I have inside UuidGenerator.groovy

import org.hibernate.id.IdentifierGenerator;
import  org.hibernate.engine.spi.SessionImplementor

import java.security.SecureRandom;

/**
 * 
 */
class UuidGenerator implements IdentifierGenerator {
    Serializable generate(SessionImplementor session, Object object) {

        UUID.randomUUID()
    }
}

For grails 2.x.x my UuidGenerator.groovy looks like this...

import org.grails.datastore.mapping.core.SessionImplementor
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.engine.SessionImplementor;


/**
 * 
 */
class UuidGenerator implements IdentifierGenerator {
    Serializable generate(SessionImplementor session, Object object) {

        return UUID.randomUUID().toString()
    }
}

And my domain class looks like this...

class MyClass {    
    static constraints = {

    }

     String id 
    static mapping = {
        id generator: "com.novadge.UuidGenerator", length:36
    }    
}



回答4:


If you specify the id as a String and generator type "uuid" you get uuid behavior for your domain in Grails 3 with little effort.

class State  {
    String id
    String state 

    static mapping = {
        id generator: 'uuid'
    }


来源:https://stackoverflow.com/questions/23736208/using-an-uuid-or-guid-as-id-in-grails-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!