Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:06:31+00:00 2026-05-27T10:06:31+00:00

So I’m trying to make a grails app that hooks into an existing DB

  • 0

So I’m trying to make a grails app that hooks into an existing DB where the primary key column is a UUID:

      Column       |            Type             | Modifiers 
-------------------+-----------------------------+-----------
 uuid              | uuid                        | not null

However, when I set up my data source like this:

class DataStore {
UUID     uuid
...
static mapping = {
...
     id generator: 'assigned', name: 'uuid', type: 'pg-uuid'
}

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.

I’m also trying to do the same with an inet column, but I figure one step at a time here.

Any help here? I’m at wit’s end.

Edit: I found this grails using uuid as id and mapping to to binary column , but it just throws this error when I try to run it now:

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

UUIDUserType.groovy

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 };
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:06:31+00:00Added an answer on May 27, 2026 at 10:06 am

    EDIT: This solution requires PostgreSQL 8.4 or higher, the according JDBC driver 8.4-703 or higher and Hibernate 3.6 (for the built-in pg-uuid type, a custom UserType should work with prior Hibernate versions)

    Just found out that you don’t need a custom UserType to map java.util.UUID to a PostgreSQL uuid. Since Hibernate 3.6 you can use the built-in type pg-uuid which is a shortcut for org.hibernate.type.PostgresUUIDType (see the Hibernate 3.6 docs about basic types). This built-in type should exactly do what a custom UserType might do.

    But I think you must use a uuid2 generator instead of the uuid generator. See the Hibernate 3.6 docs about generators. The uuid generator creates a String representation of the UUID, whereas the uuid2 generator is capable of generating values as java.util.UUID, java.lang.String or as a byte array of length 16 (byte[16]). I don’t know how to configure the uuid2 generator, though. I do not use this generator but just assign a random UUID in the constructor of my Entity base class.

    So, I think you must change your code to the following:

    class DataStore {
    UUID     uuid
    ...
    static mapping = {
    ...
      id generator: 'assigned', name: 'uuid2', type: 'pg-uuid'
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.