GORM:使用字符串作为键的一对一表映射

the*_*guy 6 grails grails-orm

我正在尝试使用一对一映射创建一对GORM域对象,但需要注意的是两个表之间的键不是很长,而是UUID字符串/ varchar.我一直在寻找关于如何在grails/gorm中执行此操作的讨论,并找到了一些问题,但没有使用答案.这是我的两个班级,我希望有人能指出我合理的方向.

谢谢.

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
}
Run Code Online (Sandbox Code Playgroud)

...由于与现有数据和表等有关的原因,使用ItemLastChange表使用item_id,因为FK不是一个可行的解决方案(尽可能多的我们希望它...)

这会导致以下错误:

java.sql.SQLException:getLong()的值无效 - '6890daf634873fbaac307cad258561be'

其中值'6890daf634873fbaac307cad258561be'是ItemLastChange表中的varchar UUID.

根据下面的评论,这是一个粗略的架构:

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  
Run Code Online (Sandbox Code Playgroud)

这些表之间没有定义的FK关系.

提前致谢.

-Steighton

Lui*_*ñiz 0

尝试将您的 id 声明为字符串:

String id

static mapping = {
    [...]
    id column:'item_id', generator: 'assigned'
    [...]
}
Run Code Online (Sandbox Code Playgroud)