What is the best way to determine if user default identifiers are created in Grails?

I want to switch my domain classes to use variable length UUIDs for my identifiers. I don’t want to just display consecutive identifiers in the URL for people I can try to contact. I wrote a custom version of the Java UUID method to allow variable lengths, so I can have shorter identifiers for models that won't get big.

I found this thread that explained how to change the default mapping so that I can switch to "assigned". Change ID generation for Grails plugin

What is the best way to configure default beforeInsert (to generate a custom UUID) and tell Grails I want to use strings for identifiers instead of integers?

I tried adding grails.gorm.default.beforeInsert to the configuration, but this did not work.

+5
source share
2 answers

To force grails to use strings for identifiers, simply declare the property String id. To populate it with a custom UUID, I would use the sleep id generator instead of beforeInsert. Create a class that extends org.hibernate.id.IdentifierGenerator, and then add an id identifier mapping to your domain class as follows:

class MyIdGenerator extends IdentifierGenerator {
    Serializable generate(SessionImplementor session, Object object) {
        return MyUUID.generate()
    }
}

class MyDomain {
    String id
    static mapping = {
        id generator:"my.package.MyIdGenerator", column:"id", unique:"true"
    }
}
+7
source

, . , Grails 2.3. ( postgres, "pg-uuid". ).

UUID uuid    
static mapping = {
    uuid generator: 'uuid2', type: 'pg-uuid'
    ...
}
+2

All Articles