gpt4 book ai didi

grails - 戈尔姆 : one-to-one table mapping using string as a key

转载 作者:行者123 更新时间:2023-12-02 07:06:50 25 4
gpt4 key购买 nike

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

谢谢。

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
}

...出于与现有数据和表等相关的原因,让 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

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

提前致谢。

-史泰顿

最佳答案

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

String id

static mapping = {
[...]
id column:'item_id', generator: 'assigned'
[...]
}

关于grails - 戈尔姆 : one-to-one table mapping using string as a key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com