6

I'm trying to create a pair of GORM domain objects with a one-to-one mapping with the caveat that the key between the two tables is not a long, but instead a UUID string/varchar. I've looked all over for discussions on how to do this in grails/gorm, and found a few questions, but no answers of use. Here are my two classes, I hope someone can point me in a reasonable direction.

Thanks.

class ItemLastChange  {
    static belongsTo = [item: Item]

    static mapping = {
        cache true
        version false
        id column: 'item_hash_msb', name: 'itemHashMsb'
        item column: 'item_uuid', key: 'uuid', sqlType: 'text', type: 'text', insertable: false, updateable: false
    }

    static constraints = {
    }

    long itemHashMsb;
    String itemUuid;
    String itemMapCode;
    String dbActionType;
    String version;
    Calendar modificationDate;
}

class Item {
    static hasOne = [itemLastChange: ItemLastChange]

    static mapping = {
        cache true
        version false
        id column:'item_id'

        columns {
            itemLastChange column: 'uuid', lazy: false, key: 'item_uuid', type: 'text', insertable: false, updateable: false
        }

    }

    static constraints = {
    }

    ItemLastChange itemLastchange

    long id
    String uuid
    //other fields eliminated
}

... for reasons relating to existing data and tables, etc. having the ItemLastChange table utilize the item_id as the FK is not a doable solution (as much as we all wish it was...)

This results in the following error:

java.sql.SQLException: Invalid value for getLong() - '6890daf634873fbaac307cad258561be'

Where the value '6890daf634873fbaac307cad258561be' is the varchar UUID from the ItemLastChange table.

Per comments below, here's a rough schema:

Item
----
Field       Type *      Collation   Null    Key     
item_id     int(11)             NO  PRI
item_type_id    int(11)     {null}      YES MUL 
organization_id int(11)     {null}      YES MUL 
item_map_code   varchar(36) utf8_bin    YES     
uuid        char(32)    utf8_bin    NO  UNI 
name        varchar(64) utf8_bin    NO      
...

ItemLastChange
--------------
Field       Type        Collation   Null    Key
item_hash_msb   bigint(20)          NO  PRI
item_uuid       varchar(32) utf8_bin    NO  UNI
item_map_code   varchar(36) utf8_bin    NO  
db_action_type  varchar(64) utf8_bin    NO  
item_version    varchar(16) utf8_bin    NO  
description     longtext    utf8_bin    YES 
ine_app_version varchar(16) utf8_bin    YES 
creation_date   datetime            NO  
modification_date   datetime            NO  

There is no defined FK relationship between these tables.

Thanks in advance.

-Steighton

  • 3
    Post the schema of the tables. – James Kleeh Aug 14 '13 at 03:39
  • 3
    What error are you getting ? – Sudhir N Aug 14 '13 at 07:02
  • Sorry, got away from this issue for a minute... (and somehow didn't see your replies... anyhow, the error I get is: java.sql.SQLException: Invalid value for getLong() - '6890daf634873fbaac307cad258561be' It appears that since the relationship is defined by a varchar key that the system is assuming that the column for the relationship *must* be a long. – the baldheadedguy Sep 12 '13 at 23:58
  • 3
    shouldn't `id` be defined as a String in Item? (String id) – aldrin Nov 22 '13 at 14:41

1 Answers1

0

Try declaring your id as a String:

String id

static mapping = {
    [...]
    id column:'item_id', generator: 'assigned'
    [...]
}
Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
  • I can't change the declaration of my id to String, but I will try changing the ID column from being the long to the uuid column to see if that works. – the baldheadedguy Mar 13 '14 at 21:34