Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

I think there is a easy way:

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

static mapping = {
    id generator:'assigned'
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top