3

I'd like to use a 128-bit UUID rather than Long for the id field on all of my Grails domains. I'd rather not have to specify all of the mapping information on every domain. Is there a simple way to achieve this in a generic/global way? I'm using Grails 2.3.x, the Hibernate 3.6.10.2 plugin, the Database Migration Plugin 1.3.8, and Oracle 11g (11.2.0.2.0).

There seem to be a number of questions related to this, but none provide complete, accurate, and up-to-date answers that actually work.

Related Questions

2 Answers 2

11

Using UUID and RAW(16)

If you want to use a UUID in your Grails domain and a RAW(16) in your database, you'll need to add the following.

  1. For every domain, specify the id field. Here's an example using ExampleDomain.groovy

    class ExampleDomain {
        UUID id
    }
    
  2. Add the following mapping to Config.groovy

    grails.gorm.default.mapping = {
        id(generator: "uuid2", type: "uuid-binary", length: 16)
    }
    

    For details on the three values I've selected, please see these links.

  3. Add a custom dialect to your data source entry in Datasource.groovy. If you are using Hibernate 4.0.0.CR5 or higher, you can skip this step.

    dataSource {
        // Other configuration values removed for brevity
        dialect = com.example.hibernate.dialect.BinaryAwareOracle10gDialect
    }
    
  4. Implement the custom dialect you referenced in step #3. Here is BinaryAwareOracle10gDialect implemented in Java. If you are using Hibernate 4.0.0.CR5 or higher, you can skip this step.

    package com.example.hibernate.dialect;
    
    import java.sql.Types;
    import org.hibernate.dialect.Oracle10gDialect;
    
    public class BinaryAwareOracle10gDialect extends Oracle10gDialect {
        @Override
        protected void registerLargeObjectTypeMappings() {
            super.registerLargeObjectTypeMappings();
            registerColumnType(Types.BINARY, 2000, "raw($l)");
            registerColumnType(Types.BINARY, "long raw");
        }
    }
    

    For more information about this change, please see the related Hibernate defect https://hibernate.atlassian.net/browse/HHH-6188.

Using UUID and VARCHAR2(36)

If you want to use a UUID in your Grails domain and a VARCHAR2(36) in your database, you'll need to add the following.

  1. For every domain, specify the id field. Here's an example using ExampleDomain.groovy.

    class ExampleDomain {
        UUID id
    }
    
  2. Add the following mapping to Config.groovy

    grails.gorm.default.mapping = {
        id(generator: "uuid2", type: "uuid-char", length: 36)
    }
    

    For details on the three values, please see the links in step #2 from the previous section.

3
  • thanks for the info. Works great. But how did you know to use the "type" and "length" map entries? For example, how can I find out what other options are available? I don't know who is consuming those values and I don't see them all documented in Grails mapping for id: grails.org/doc/latest/ref/Database%20Mapping/id.html May 9, 2014 at 2:07
  • I had to dig through the Hibernate documentation. Some of the mapping information is available here: docs.jboss.org/hibernate/core/3.6/reference/en-US/html/…. Also, if you found my original answer useful, please upvote it!
    – jstricker
    May 12, 2014 at 15:32
  • Are you talking about the section labeled "5.1.4.1.4. Declaring column attributes"? The "Mapping id()" Grails docs page I pasted above specifies certain options that can be set for id()'s mapping block, but it doesn't mention "length" or "type", and probably others. So I'm guessing any entries not documented on that Grails page (like "generator" or "name") will get handled as Hibernate column/property attributes? In any case, thank you again! (upvoted) May 12, 2014 at 19:35
0

I think there is a easy way:

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

static mapping = {
    id generator:'assigned'
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.