title: Configuring Grails/Hibernate/Postgres for UUID
body: <p>So I'm trying to make a grails app that hooks into an existing DB where the primary key column is a UUID:</p>
  
  <pre><code>      Column       |            Type             | Modifiers 
  -------------------+-----------------------------+-----------
   uuid              | uuid                        | not null
  </code></pre>
  
  <p>However, when I set up my data source like this:</p>
  
  <pre><code>class DataStore {
  UUID     uuid
  ...
  static mapping = {
  ...
       id generator: 'assigned', name: 'uuid', type: 'pg-uuid'
  }
  </code></pre>
  
  <p>It insists that the uuid column is a varchar(255).  I'm not sure what I have to do to make it recognize that the uuid column is a uuid column, and I've tried putting a UserType class in src/groovy/, but that didn't fix anything.</p>
  
  <p>I'm also trying to do the same with an inet column, but I figure one step at a time here.</p>
  
  <p>Any help here?  I'm at wit's end.</p>
  
  <p>Edit:  I found this <a href="https://stackoverflow.com/questions/5129236/grails-using-uuid-as-id-and-mapping-to-to-binary-column">grails using uuid as id and mapping to to binary column</a> , but it just throws this error when I try to run it now:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: Error evaluating ORM mappings block for domain [cstools.domain.DataStore]:  No such property: UUIDUserType for class: org.codehaus.groovy.grails.orm.hibernate.cfg.HibernateMappingBuilder
  ... 23 more
  </code></pre>
  
  <p>UUIDUserType.groovy</p>
  
  <pre><code>import java.io.Serializable;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Types;
  import java.util.UUID;
  import org.hibernate.HibernateException;
  
  public class UUIDUserType implements org.hibernate.usertype.UserType {
  
      private static final String CAST_EXCEPTION_TEXT = " cannot be cast to a java.util.UUID.";
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
       *      java.lang.Object)
       */
      public Object assemble( Serializable cached, Object owner ) throws HibernateException {
  
          if ( !String.class.isAssignableFrom( cached.getClass() ) ) {
              return null;
          }
  
          return UUID.fromString( (String) cached );
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
       */
      public Object deepCopy( Object value ) throws HibernateException {
  
          if ( !UUID.class.isAssignableFrom( value.getClass() ) ) {
              throw new HibernateException( value.getClass().toString() + CAST_EXCEPTION_TEXT );
          }
  
          UUID other = (UUID) value;
  
          return UUID.fromString( other.toString() );
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
       */
      public Serializable disassemble( Object value ) throws HibernateException {
  
          return value.toString();
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#equals(java.lang.Object,
       *      java.lang.Object)
       */
      public boolean equals( Object x, Object y ) throws HibernateException {
  
          if ( !UUID.class.isAssignableFrom( x.getClass() ) ) {
              throw new HibernateException( x.getClass().toString() + CAST_EXCEPTION_TEXT );
          }
          else if ( !UUID.class.isAssignableFrom( y.getClass() ) ) {
              throw new HibernateException( y.getClass().toString() + CAST_EXCEPTION_TEXT );
          }
  
          UUID a = (UUID) x;
          UUID b = (UUID) y;
  
          return a.equals( b );
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
       */
      public int hashCode( Object x ) throws HibernateException {
          if ( !UUID.class.isAssignableFrom( x.getClass() ) ) {
              throw new HibernateException( x.getClass().toString() + CAST_EXCEPTION_TEXT );
          }
  
          UUID a = (UUID) x;
  
          return a.hashCode();
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#isMutable()
       */
      public boolean isMutable() {
  
          return false;
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
       *      java.lang.String[], java.lang.Object)
       */
      public Object nullSafeGet( ResultSet rs, String[] names, Object owner ) throws HibernateException, SQLException {
  
          String value = rs.getString( names[0] );
          if ( value == null ) {
              return null;
          }
          else {
              return UUID.fromString( value );
          }
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
       *      java.lang.Object, int)
       */
      public void nullSafeSet( PreparedStatement st, Object value, int index ) throws HibernateException, SQLException {
  
          if ( value == null ) {
              st.setNull( index, Types.VARCHAR );
              return;
          }
  
          if ( !UUID.class.isAssignableFrom( value.getClass() ) ) {
              throw new HibernateException( value.getClass().toString() + CAST_EXCEPTION_TEXT );
          }
  
          st.setString( index, value.toString() );
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#replace(java.lang.Object,
       *      java.lang.Object, java.lang.Object)
       */
      public Object replace( Object original, Object target, Object owner ) throws HibernateException {
  
          if ( !UUID.class.isAssignableFrom( original.getClass() ) ) {
              throw new HibernateException( original.getClass().toString() + CAST_EXCEPTION_TEXT );
          }
  
          return UUID.fromString( original.toString() );
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#returnedClass()
       */
      @SuppressWarnings( "unchecked" )
      public Class returnedClass() {
  
          return UUID.class;
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see org.hibernate.usertype.UserType#sqlTypes()
       */
      public int[] sqlTypes() {
  
          return int[] { Types.CHAR };
      }
  }
  </code></pre>