How can I configure Grails id columns to use UUID on Oracle?
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.
For every domain, specify the id field. Here's an example using
ExampleDomain.groovy
class ExampleDomain { UUID id}
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.
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}
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.
For every domain, specify the id field. Here's an example using
ExampleDomain.groovy
.class ExampleDomain { UUID id}
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.
I think there is a easy way:
String id = UUID.randomUUID().toString()static mapping = { id generator:'assigned'}